-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathpage.tsx
109 lines (95 loc) · 2.78 KB
/
page.tsx
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
'use client';
import { useFormState } from 'react-dom';
import { signIn, verifyTotp } from './mfa';
import Image from 'next/image';
export default function Mfa() {
// This example uses Next.js server actions to call functions on the server side.
//
// If your application is a single page app (SPA), you will need to:
// - handle the form submission in `<form onSubmit>`
// - make an API call to your backend (e.g using `fetch`)
const [signInState, signInAction] = useFormState(signIn, { error: null });
const [verifyState, verifyAction] = useFormState(verifyTotp, { error: null });
if (!('authenticationChallenge' in signInState) || 'user' in signInState) {
return (
<main key="sign-in">
<h1>Multi-Factor Auth</h1>
<h2>Sign-in</h2>
<form action={signInAction}>
<div>
<label htmlFor="email">Email</label>
<input
type="email"
name="email"
id="email"
autoCapitalize="off"
autoComplete="username"
autoFocus
required
/>
</div>
<div>
<label htmlFor="password">Password</label>
<input
type="password"
name="password"
id="password"
autoCapitalize="off"
autoComplete="current-password"
required
/>
</div>
<button type="submit">Sign-in</button>
</form>
<pre>{JSON.stringify(signInState, null, 2)}</pre>
</main>
);
}
return (
<main key="mfa">
<h1>Multi-Factor Auth</h1>
{signInState.authenticationFactor ? (
<>
<h2>Enroll</h2>
<p>Scan the QR code</p>
<Image
src={signInState.authenticationFactor.totp.qrCode}
width="160"
height="160"
alt="QR code"
/>
<p>then</p>
</>
) : (
<h2>Verify</h2>
)}
<form action={verifyAction}>
<div>
<label htmlFor="code">Enter the code from your app</label>
<input
type="text"
name="code"
id="code"
inputMode="numeric"
autoComplete="one-time-code"
pattern="^\d{6}$"
autoFocus
required
/>
</div>
<input
type="hidden"
name="authenticationChallengeId"
value={signInState.authenticationChallenge.id}
/>
<input
type="hidden"
name="pendingAuthenticationToken"
value={signInState.pendingAuthenticationToken}
/>
<button type="submit">Continue</button>
</form>
<pre>{JSON.stringify(verifyState, null, 2)}</pre>
</main>
);
}