|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Dialog } from '@/components/elements/dialog'; |
| 3 | +import { DialogProps } from '@/components/elements/dialog/Dialog'; |
| 4 | +import getTwoFactorTokenData, { TwoFactorTokenData } from '@/api/account/getTwoFactorTokenData'; |
| 5 | +import { useFlashKey } from '@/plugins/useFlash'; |
| 6 | +import tw from 'twin.macro'; |
| 7 | +import QRCode from 'qrcode.react'; |
| 8 | +import { Button } from '@/components/elements/button/index'; |
| 9 | +import Spinner from '@/components/elements/Spinner'; |
| 10 | +import { Input } from '@/components/elements/inputs'; |
| 11 | +import CopyOnClick from '@/components/elements/CopyOnClick'; |
| 12 | +import Tooltip from '@/components/elements/tooltip/Tooltip'; |
| 13 | +import enableAccountTwoFactor from '@/api/account/enableAccountTwoFactor'; |
| 14 | +import FlashMessageRender from '@/components/FlashMessageRender'; |
| 15 | +import RecoveryTokensDialog from '@/components/dashboard/forms/RecoveryTokensDialog'; |
| 16 | +import { Actions, useStoreActions } from 'easy-peasy'; |
| 17 | +import { ApplicationStore } from '@/state'; |
| 18 | + |
| 19 | +type SetupTOTPModalProps = DialogProps; |
| 20 | + |
| 21 | +export default ({ open, onClose }: SetupTOTPModalProps) => { |
| 22 | + const [submitting, setSubmitting] = useState(false); |
| 23 | + const [value, setValue] = useState(''); |
| 24 | + const [tokens, setTokens] = useState<string[]>([]); |
| 25 | + const [token, setToken] = useState<TwoFactorTokenData | null>(null); |
| 26 | + const { clearAndAddHttpError } = useFlashKey('account:two-step'); |
| 27 | + const updateUserData = useStoreActions((actions: Actions<ApplicationStore>) => actions.user.updateUserData); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (!open) return; |
| 31 | + |
| 32 | + getTwoFactorTokenData() |
| 33 | + .then(setToken) |
| 34 | + .then(() => updateUserData({ useTotp: true })) |
| 35 | + .catch((error) => clearAndAddHttpError(error)); |
| 36 | + }, [open]); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (!open) return; |
| 40 | + |
| 41 | + return () => { |
| 42 | + setToken(null); |
| 43 | + setValue(''); |
| 44 | + setSubmitting(false); |
| 45 | + clearAndAddHttpError(undefined); |
| 46 | + }; |
| 47 | + }, [open]); |
| 48 | + |
| 49 | + const submit = () => { |
| 50 | + if (submitting) return; |
| 51 | + |
| 52 | + setSubmitting(true); |
| 53 | + clearAndAddHttpError(); |
| 54 | + |
| 55 | + enableAccountTwoFactor(value) |
| 56 | + .then(setTokens) |
| 57 | + .catch(clearAndAddHttpError) |
| 58 | + .then(() => setSubmitting(false)); |
| 59 | + }; |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <RecoveryTokensDialog tokens={tokens} open={open && tokens.length > 0} onClose={onClose} /> |
| 64 | + <Dialog |
| 65 | + open={open && !tokens.length} |
| 66 | + onClose={onClose} |
| 67 | + title={'Enable Two-Step Verification'} |
| 68 | + preventExternalClose={submitting} |
| 69 | + description={ |
| 70 | + "Help protect your account from unauthorized access. You'll be prompted for a verification code each time you sign in." |
| 71 | + } |
| 72 | + > |
| 73 | + <FlashMessageRender byKey={'account:two-step'} className={'mt-4'} /> |
| 74 | + <div |
| 75 | + className={ |
| 76 | + 'flex items-center justify-center w-56 h-56 p-2 bg-gray-800 rounded-lg shadow mx-auto mt-6' |
| 77 | + } |
| 78 | + > |
| 79 | + {!token ? ( |
| 80 | + <Spinner /> |
| 81 | + ) : ( |
| 82 | + <QRCode |
| 83 | + renderAs={'svg'} |
| 84 | + value={token.image_url_data} |
| 85 | + css={tw`w-full h-full shadow-none rounded`} |
| 86 | + /> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + <CopyOnClick text={token?.secret}> |
| 90 | + <p className={'font-mono text-sm text-gray-100 text-center mt-2'}> |
| 91 | + {token?.secret.match(/.{1,4}/g)!.join(' ') || 'Loading...'} |
| 92 | + </p> |
| 93 | + </CopyOnClick> |
| 94 | + <div className={'mt-6'}> |
| 95 | + <p> |
| 96 | + Scan the QR code above using the two-step authentication app of your choice. Then, enter the |
| 97 | + 6-digit code generated into the field below. |
| 98 | + </p> |
| 99 | + </div> |
| 100 | + <Input.Text |
| 101 | + variant={Input.Text.Variants.Loose} |
| 102 | + value={value} |
| 103 | + onChange={(e) => setValue(e.currentTarget.value)} |
| 104 | + className={'mt-4'} |
| 105 | + placeholder={'000000'} |
| 106 | + type={'text'} |
| 107 | + inputMode={'numeric'} |
| 108 | + autoComplete={'one-time-code'} |
| 109 | + pattern={'\\d{6}'} |
| 110 | + /> |
| 111 | + <Dialog.Footer> |
| 112 | + <Button.Text onClick={onClose}>Cancel</Button.Text> |
| 113 | + <Tooltip |
| 114 | + disabled={value.length === 6} |
| 115 | + content={ |
| 116 | + !token ? 'Waiting for QR code to load...' : 'You must enter the 6-digit code to continue.' |
| 117 | + } |
| 118 | + delay={100} |
| 119 | + > |
| 120 | + <Button disabled={!token || value.length !== 6} onClick={submit}> |
| 121 | + Enable |
| 122 | + </Button> |
| 123 | + </Tooltip> |
| 124 | + </Dialog.Footer> |
| 125 | + </Dialog> |
| 126 | + </> |
| 127 | + ); |
| 128 | +}; |
0 commit comments