Files
notesnook/apps/mobile/app/components/auth/change-password.js

138 lines
3.6 KiB
JavaScript
Raw Normal View History

import React, { useRef, useState } from "react";
import { View } from "react-native";
import { useThemeStore } from "../../stores/use-theme-store";
import { useUserStore } from "../../stores/use-user-store";
import {
eSendEvent,
presentSheet,
ToastEvent
} from "../../services/event-manager";
import { db } from "../../common/database";
import { eCloseProgressDialog } from "../../utils/events";
import { Button } from "../ui/button";
import DialogHeader from "../dialog/dialog-header";
import Input from "../ui/input";
import { Notice } from "../ui/notice";
import Seperator from "../ui/seperator";
2022-02-07 14:44:48 +05:00
export const ChangePassword = () => {
const colors = useThemeStore((state) => state.colors);
2022-02-07 14:44:48 +05:00
const passwordInputRef = useRef();
const password = useRef();
const oldPasswordInputRef = useRef();
const oldPassword = useRef();
const [error, setError] = useState(false);
const [loading, setLoading] = useState(false);
const user = useUserStore((state) => state.user);
2022-02-07 14:44:48 +05:00
const changePassword = async () => {
2022-07-09 09:45:16 +05:00
if (!user?.isEmailConfirmed) {
2022-02-18 23:24:21 +05:00
ToastEvent.show({
heading: "Email not confirmed",
message: "Please confirm your email to change account password",
type: "error",
context: "local"
2022-02-18 23:24:21 +05:00
});
return;
}
2022-02-18 14:45:38 +05:00
if (error || !oldPassword.current || !password.current) {
2022-02-07 14:44:48 +05:00
ToastEvent.show({
heading: "All fields required",
message: "Fill all the fields and try again.",
type: "error",
context: "local"
2022-02-07 14:44:48 +05:00
});
return;
}
setLoading(true);
try {
2022-02-18 23:24:21 +05:00
await db.user.clearSessions();
2022-02-18 14:45:38 +05:00
await db.user.changePassword(oldPassword.current, password.current);
2022-02-07 14:44:48 +05:00
ToastEvent.show({
heading: `Account password updated`,
type: "success",
context: "global"
2022-02-07 14:44:48 +05:00
});
2022-02-18 23:24:21 +05:00
setLoading(false);
eSendEvent(eCloseProgressDialog);
2022-02-07 14:44:48 +05:00
} catch (e) {
2022-02-18 23:24:21 +05:00
setLoading(false);
2022-02-07 14:44:48 +05:00
ToastEvent.show({
heading: "Failed to change password",
2022-02-07 14:44:48 +05:00
message: e.message,
type: "error",
context: "local"
2022-02-07 14:44:48 +05:00
});
}
setLoading(false);
};
return (
<View
style={{
width: "100%",
2022-02-07 14:44:48 +05:00
padding: 12
}}
>
<DialogHeader
title="Change password"
paragraph="Enter your old and new passwords"
/>
2022-02-07 14:44:48 +05:00
<Seperator />
<Input
fwdRef={oldPasswordInputRef}
onChangeText={(value) => {
2022-02-07 14:44:48 +05:00
oldPassword.current = value;
}}
returnKeyLabel="Next"
returnKeyType="next"
secureTextEntry
2022-02-28 13:48:59 +05:00
autoComplete="password"
2022-02-07 14:44:48 +05:00
autoCapitalize="none"
autoCorrect={false}
placeholder="Old Password"
/>
<Input
fwdRef={passwordInputRef}
onChangeText={(value) => {
2022-02-07 14:44:48 +05:00
password.current = value;
}}
onErrorCheck={(e) => setError(e)}
2022-02-07 14:44:48 +05:00
returnKeyLabel="Next"
returnKeyType="next"
secureTextEntry
validationType="password"
2022-02-28 13:48:59 +05:00
autoComplete="password"
2022-02-07 14:44:48 +05:00
autoCapitalize="none"
autoCorrect={false}
placeholder="New password"
/>
<Notice
2022-02-18 23:24:21 +05:00
text="Changing password is a non-undoable process. You will be logged out from all your devices. Please make sure you do not close the app while your password is changing and have good internet connection."
2022-02-07 14:44:48 +05:00
type="alert"
/>
<Button
style={{
marginTop: 10,
width: "100%"
2022-02-07 14:44:48 +05:00
}}
loading={loading}
onPress={changePassword}
type="accent"
title={loading ? null : "I understand, change my password"}
2022-02-07 14:44:48 +05:00
/>
</View>
);
};
ChangePassword.present = () => {
presentSheet({
component: <ChangePassword />
});
};