-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathMfaBackupCodeCreateForm.tsx
More file actions
70 lines (60 loc) · 2.5 KB
/
Copy pathMfaBackupCodeCreateForm.tsx
File metadata and controls
70 lines (60 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { isReverificationCancelledError } from '@clerk/shared/error';
import { useReverification, useUser } from '@clerk/shared/react';
import type { BackupCodeResource } from '@clerk/shared/types';
import React from 'react';
import { useCardState, withCardStateProvider } from '@/ui/elements/contexts';
import { FormButtonContainer } from '@/ui/elements/FormButtons';
import type { FormProps } from '@/ui/elements/FormContainer';
import { FormContainer } from '@/ui/elements/FormContainer';
import { FullHeightLoader } from '@/ui/elements/FullHeightLoader';
import { handleError } from '@/ui/utils/errorHandler';
import { Button, descriptors, localizationKeys, Text, useAppearance } from '../../customizables';
import { MfaBackupCodeList } from './MfaBackupCodeList';
type MfaBackupCodeCreateFormProps = FormProps;
export const MfaBackupCodeCreateForm = withCardStateProvider((props: MfaBackupCodeCreateFormProps) => {
const { onSuccess, onReset } = props;
const { user } = useUser();
const card = useCardState();
const { autoFocus: optionAutoFocus } = useAppearance().parsedOptions;
const createBackupCode = useReverification(() => user?.createBackupCode());
const [backupCode, setBackupCode] = React.useState<BackupCodeResource | undefined>(undefined);
React.useEffect(() => {
if (backupCode || !user) {
return;
}
void createBackupCode()
.then(backupCode => setBackupCode(backupCode))
.catch(err => {
if (isReverificationCancelledError(err)) {
return onReset();
}
handleError(err, [], card.setError);
});
}, []);
if (card.error) {
return <FormContainer headerTitle={localizationKeys('userProfile.backupCodePage.title')} />;
}
return (
<FormContainer headerTitle={localizationKeys('userProfile.backupCodePage.title')}>
{!backupCode ? (
<FullHeightLoader />
) : (
<>
<Text localizationKey={localizationKeys('userProfile.backupCodePage.successMessage')} />
<MfaBackupCodeList
subtitle={localizationKeys('userProfile.backupCodePage.subtitle__codelist')}
backupCodes={backupCode.codes}
/>
<FormButtonContainer>
<Button
autoFocus={optionAutoFocus}
onClick={onSuccess}
localizationKey={localizationKeys('userProfile.formButtonPrimary__finish')}
elementDescriptor={descriptors.formButtonPrimary}
/>
</FormButtonContainer>
</>
)}
</FormContainer>
);
});