import React, { useEffect, useState } from 'react'; import { Dialog } from '@/components/elements/dialog'; import { DialogProps } from '@/components/elements/dialog/Dialog'; import getTwoFactorTokenData, { TwoFactorTokenData } from '@/api/account/getTwoFactorTokenData'; import { useFlashKey } from '@/plugins/useFlash'; import tw from 'twin.macro'; import QRCode from 'qrcode.react'; import { Button } from '@/components/elements/button/index'; import Spinner from '@/components/elements/Spinner'; import { Input } from '@/components/elements/inputs'; import CopyOnClick from '@/components/elements/CopyOnClick'; import Tooltip from '@/components/elements/tooltip/Tooltip'; import enableAccountTwoFactor from '@/api/account/enableAccountTwoFactor'; import FlashMessageRender from '@/components/FlashMessageRender'; import RecoveryTokensDialog from '@/components/dashboard/forms/RecoveryTokensDialog'; import { Actions, useStoreActions } from 'easy-peasy'; import { ApplicationStore } from '@/state'; type SetupTOTPModalProps = DialogProps; export default ({ open, onClose }: SetupTOTPModalProps) => { const [submitting, setSubmitting] = useState(false); const [value, setValue] = useState(''); const [tokens, setTokens] = useState([]); const [token, setToken] = useState(null); const { clearAndAddHttpError } = useFlashKey('account:two-step'); const updateUserData = useStoreActions((actions: Actions) => actions.user.updateUserData); useEffect(() => { if (!open) return; getTwoFactorTokenData() .then(setToken) .then(() => updateUserData({ useTotp: true })) .catch((error) => clearAndAddHttpError(error)); }, [open]); useEffect(() => { if (!open) return; return () => { setToken(null); setValue(''); setSubmitting(false); clearAndAddHttpError(undefined); }; }, [open]); const submit = () => { if (submitting) return; setSubmitting(true); clearAndAddHttpError(); enableAccountTwoFactor(value) .then(setTokens) .catch(clearAndAddHttpError) .then(() => setSubmitting(false)); }; return ( <> 0} onClose={onClose} />
{!token ? ( ) : ( )}

{token?.secret.match(/.{1,4}/g)!.join(' ') || 'Loading...'}

Scan the QR code above using the two-step authentication app of your choice. Then, enter the 6-digit code generated into the field below.

setValue(e.currentTarget.value)} className={'mt-4'} placeholder={'000000'} type={'text'} inputMode={'numeric'} autoComplete={'one-time-code'} pattern={'\\d{6}'} /> Cancel
); };