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

168 lines
4.6 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2022-08-29 16:19:17 +05:00
import React, { useRef, useState } from "react";
import { View } from "react-native";
2022-08-29 16:19:17 +05:00
import { db } from "../../common/database";
import {
eSendEvent,
presentSheet,
2023-08-29 20:42:45 +05:00
ToastManager
} from "../../services/event-manager";
2022-08-29 16:19:17 +05:00
import { useUserStore } from "../../stores/use-user-store";
import { eCloseSheet, eOpenRecoveryKeyDialog } from "../../utils/events";
import DialogHeader from "../dialog/dialog-header";
2022-08-29 16:19:17 +05:00
import { Button } from "../ui/button";
import Input from "../ui/input";
import { Notice } from "../ui/notice";
import Seperator from "../ui/seperator";
import { Dialog } from "../dialog";
import BackupService from "../../services/backup";
import { sleep } from "../../utils/time";
2024-07-27 10:19:43 +05:00
import { strings } from "@notesnook/intl";
2022-02-07 14:44:48 +05:00
export const ChangePassword = () => {
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) {
2023-08-29 20:42:45 +05:00
ToastManager.show({
2024-07-27 10:19:43 +05:00
heading: strings.emailNotConfirmed(),
message: strings.emailNotConfirmedDesc(),
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) {
2023-08-29 20:42:45 +05:00
ToastManager.show({
2024-07-27 10:19:43 +05:00
heading: strings.allFieldsRequired(),
message: strings.allFieldsRequiredDesc(),
type: "error",
context: "local"
2022-02-07 14:44:48 +05:00
});
return;
}
setLoading(true);
try {
2024-12-13 14:07:15 +05:00
const result = await BackupService.run(
false,
"change-password-dialog",
"partial"
);
if (result.error) {
2024-07-27 10:19:43 +05:00
throw new Error(strings.backupFailed() + `: ${result.error}`);
2024-12-13 14:07:15 +05:00
}
2024-03-04 16:30:34 +05:00
2022-02-18 14:45:38 +05:00
await db.user.changePassword(oldPassword.current, password.current);
2023-08-29 20:42:45 +05:00
ToastManager.show({
2024-07-27 10:19:43 +05:00
heading: strings.passwordChangedSuccessfully(),
type: "success",
context: "global"
2022-02-07 14:44:48 +05:00
});
2022-02-18 23:24:21 +05:00
setLoading(false);
2023-01-03 10:23:48 +05:00
eSendEvent(eCloseSheet);
await sleep(300);
eSendEvent(eOpenRecoveryKeyDialog);
2022-02-07 14:44:48 +05:00
} catch (e) {
2022-02-18 23:24:21 +05:00
setLoading(false);
2023-08-29 20:42:45 +05:00
ToastManager.show({
2024-07-27 10:19:43 +05:00
heading: strings.passwordChangeFailed(),
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
}}
>
<Dialog context="change-password-dialog" />
2024-07-27 10:19:43 +05:00
<DialogHeader title={strings.changePassword()} />
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}
2024-08-13 15:13:46 +05:00
placeholder={strings.oldPassword()}
2022-02-07 14:44:48 +05:00
/>
<Input
fwdRef={passwordInputRef}
onChangeText={(value) => {
2022-02-07 14:44:48 +05:00
password.current = value;
}}
onErrorCheck={(e) => setError(e)}
2024-08-13 15:13:46 +05:00
returnKeyLabel={strings.next()}
2022-02-07 14:44:48 +05:00
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}
2024-08-13 15:13:46 +05:00
placeholder={strings.newPassword()}
2022-02-07 14:44:48 +05:00
/>
2024-07-27 10:19:43 +05:00
<Notice text={strings.changePasswordNotice()} type="alert" />
<View style={{ height: 10 }} />
2024-07-27 10:19:43 +05:00
<Notice text={strings.changePasswordNotice2()} type="alert" />
2022-02-07 14:44:48 +05:00
<Button
style={{
marginTop: 10,
width: "100%"
2022-02-07 14:44:48 +05:00
}}
loading={loading}
onPress={changePassword}
type="accent"
2024-07-27 10:19:43 +05:00
title={loading ? null : strings.changePasswordConfirm()}
2022-02-07 14:44:48 +05:00
/>
</View>
);
};
ChangePassword.present = () => {
presentSheet({
component: <ChangePassword />
});
};