-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetUpTotp.ts
119 lines (116 loc) · 3.34 KB
/
setUpTotp.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { callUserfront } from "../../services/userfront";
import {
AuthContext,
AuthMachineConfig,
AuthMachineEvent,
TotpCodeContext,
TotpCodeSubmitEvent,
} from "../types";
// TOTP Authenticator setup state machine config
const setUpTotpConfig: AuthMachineConfig = {
id: "setUpTotp",
initial: "getQrCode",
entry: ["clearError", "setupView"],
states: {
// First we need to get the QR code from the Userfront API,
// so we can show it
getQrCode: {
invoke: {
// @ts-ignore
src: () => callUserfront({ method: "store.user.getTotp" }),
// If there's a problem getting the QR code, show an error message
onError: {
actions: "setErrorFromApiError",
target: "showErrorMessage",
},
// Once we have the QR code, show the form
onDone: {
actions: "setQrCode",
target: "showQrCode",
},
},
},
// Show the form with QR code + field to verify it works
showQrCode: {
on: {
// Store the TOTP code the user entered so we can verify it
submit: {
actions: "setTotpCode",
target: "confirmTotpCode",
},
// Go back to the factor selection view
back: {
actions: "clearError",
target: "#backToFactors",
},
},
},
// Confirm the TOTP setup is correct by using a TOTP code
confirmTotpCode: {
entry: "clearError",
invoke: {
// Set the code and call the API method
src: (context: AuthContext<any>, event: AuthMachineEvent) =>
callUserfront({
// Should ALWAYS be Userfront.login here
method: "login",
args: [
{
method: "totp",
totpCode: (<TotpCodeSubmitEvent>event).totpCode,
email: context.user.email,
redirect: false,
},
],
}),
// On error, show the error message and return to the form
onError: {
actions: "setErrorFromApiError",
target: "showQrCode",
},
// When verified, show the backup codes so the user can record them
onDone: {
actions: "storeFactorResponse",
target: "showBackupCodes",
},
},
},
// Show the user's backup codes once TOTP setup succeeds
showBackupCodes: {
on: {
// Proceed to the second factor if required,
// otherwise show a message
finish: [
{
actions: "setAllowedSecondFactorsFromView",
target: "#beginSecondFactor",
cond: "secondFactorRequiredFromView",
},
// We're signed in.
// Instruct CoreJS to redirect as appropriate here
// Show the "verified" view in case we don't redirect.
{
actions: "redirectIfLoggedIn",
target: "showTotpSetupComplete",
},
],
},
},
// Show an error message only - if there's a problem getting
// the QR code.
showErrorMessage: {
on: {
retry: "getQrCode",
back: {
actions: "clearError",
target: "#backToFactors",
},
},
},
// Show a confirmation screen, in case we don't redirect.
showTotpSetupComplete: {
type: "final",
},
},
};
export default setUpTotpConfig;