1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-23 15:07:43 +01:00
smov/src/pages/parts/auth/VerifyPassphrasePart.tsx

121 lines
3.7 KiB
TypeScript
Raw Normal View History

import { useState } from "react";
import { useGoogleReCaptcha } from "react-google-recaptcha-v3";
import { useTranslation } from "react-i18next";
2023-11-05 01:16:45 +01:00
import { useAsyncFn } from "react-use";
import { updateSettings } from "@/backend/accounts/settings";
import { Button } from "@/components/buttons/Button";
import { Icon, Icons } from "@/components/Icon";
import {
LargeCard,
LargeCardButtons,
LargeCardText,
} from "@/components/layout/LargeCard";
import { AuthInputBox } from "@/components/text-inputs/AuthInputBox";
import { useAuth } from "@/hooks/auth/useAuth";
import { useBackendUrl } from "@/hooks/auth/useBackendUrl";
2023-11-05 01:16:45 +01:00
import { AccountProfile } from "@/pages/parts/auth/AccountCreatePart";
import { useBookmarkStore } from "@/stores/bookmarks";
import { useLanguageStore } from "@/stores/language";
import { useProgressStore } from "@/stores/progress";
import { useSubtitleStore } from "@/stores/subtitles";
import { useThemeStore } from "@/stores/theme";
2023-11-05 01:16:45 +01:00
interface VerifyPassphraseProps {
mnemonic: string | null;
hasCaptcha?: boolean;
userData: AccountProfile | null;
2023-11-05 01:16:45 +01:00
onNext?: () => void;
}
export function VerifyPassphrase(props: VerifyPassphraseProps) {
const [mnemonic, setMnemonic] = useState("");
const { register, restore, importData } = useAuth();
const progressItems = useProgressStore((store) => store.items);
const bookmarkItems = useBookmarkStore((store) => store.bookmarks);
const applicationLanguage = useLanguageStore((store) => store.language);
const defaultSubtitleLanguage = useSubtitleStore(
(store) => store.lastSelectedLanguage
);
const applicationTheme = useThemeStore((store) => store.theme);
const backendUrl = useBackendUrl();
const { t } = useTranslation();
2023-11-05 01:16:45 +01:00
const { executeRecaptcha } = useGoogleReCaptcha();
2023-11-05 01:16:45 +01:00
const [result, execute] = useAsyncFn(
async (inputMnemonic: string) => {
if (!props.mnemonic || !props.userData)
throw new Error(t("auth.verify.invalidData") ?? undefined);
let recaptchaToken: string | undefined;
if (props.hasCaptcha) {
recaptchaToken = executeRecaptcha
? await executeRecaptcha()
: undefined;
if (!recaptchaToken)
throw new Error(t("auth.verify.recaptchaFailed") ?? undefined);
}
2023-11-05 01:16:45 +01:00
if (inputMnemonic !== props.mnemonic)
throw new Error(t("auth.verify.noMatch") ?? undefined);
2023-11-05 01:16:45 +01:00
const account = await register({
mnemonic: inputMnemonic,
userData: props.userData,
recaptchaToken,
2023-11-05 01:16:45 +01:00
});
await importData(account, progressItems, bookmarkItems);
await updateSettings(backendUrl, account, {
applicationLanguage,
defaultSubtitleLanguage: defaultSubtitleLanguage ?? undefined,
applicationTheme: applicationTheme ?? undefined,
});
await restore(account);
2023-11-05 01:16:45 +01:00
props.onNext?.();
},
[props, register, restore]
2023-11-05 01:16:45 +01:00
);
return (
<LargeCard>
2023-11-22 17:00:14 +01:00
<form>
<LargeCardText
icon={<Icon icon={Icons.CIRCLE_CHECK} />}
title={t("auth.verify.title")}
>
{t("auth.verify.description")}
2023-11-22 17:00:14 +01:00
</LargeCardText>
<AuthInputBox
label={t("auth.verify.passphraseLabel") ?? undefined}
2023-11-22 17:00:14 +01:00
autoComplete="username"
name="username"
value={mnemonic}
onChange={setMnemonic}
passwordToggleable
2023-11-22 17:00:14 +01:00
/>
{result.error ? (
<p className="mt-3 text-authentication-errorText">
{result.error.message}
</p>
) : null}
<LargeCardButtons>
<Button
theme="purple"
loading={result.loading}
onClick={() => execute(mnemonic)}
>
{t("auth.verify.register")}
2023-11-22 17:00:14 +01:00
</Button>
</LargeCardButtons>
</form>
</LargeCard>
2023-11-05 01:16:45 +01:00
);
}