import { enableTwoFactor, setupTwoFactor } from "@/accounts/api/user"; import VerifyTwoFactor, { type VerifyTwoFactorCallback, } from "@/accounts/components/two-factor/VerifyForm"; import { TwoFactorSetup } from "@/accounts/components/two-factor/setup"; import type { TwoFactorSecret } from "@/accounts/types/user"; import log from "@/base/log"; import { ensure } from "@/utils/ensure"; import { VerticallyCentered } from "@ente/shared/components/Container"; import LinkButton from "@ente/shared/components/LinkButton"; import { encryptWithRecoveryKey } from "@ente/shared/crypto/helpers"; import { LS_KEYS, getData, setLSUser } from "@ente/shared/storage/localStorage"; import { Box, CardContent, Typography } from "@mui/material"; import Card from "@mui/material/Card"; import { t } from "i18next"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import { appHomeRoute } from "../../services/redirect"; import type { PageProps } from "../../types/page"; export enum SetupMode { QR_CODE, MANUAL_CODE, } const Page: React.FC = () => { const [twoFactorSecret, setTwoFactorSecret] = useState< TwoFactorSecret | undefined >(); const router = useRouter(); useEffect(() => { if (twoFactorSecret) { return; } const main = async () => { try { const twoFactorSecret = await setupTwoFactor(); setTwoFactorSecret(twoFactorSecret); } catch (e) { log.error("failed to get two factor setup code", e); } }; main(); }, []); const onSubmit: VerifyTwoFactorCallback = async ( otp: string, markSuccessful, ) => { const recoveryEncryptedTwoFactorSecret = await encryptWithRecoveryKey( ensure(twoFactorSecret).secretCode, ); await enableTwoFactor(otp, recoveryEncryptedTwoFactorSecret); await markSuccessful(); await setLSUser({ ...getData(LS_KEYS.USER), isTwoFactorEnabled: true, }); router.push(appHomeRoute); }; return ( {t("TWO_FACTOR")} {t("GO_BACK")} ); }; export default Page;