-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
213 lines (185 loc) · 7.07 KB
/
page.tsx
File metadata and controls
213 lines (185 loc) · 7.07 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"use client";
import Link from "next/link";
import { useState } from "react";
import { Amplify } from "aws-amplify";
import { signUp, confirmSignUp } from "aws-amplify/auth";
import Loader from "@/app/react-contact-form/components/loader";
import { generatePassword, validateEmail } from "@/app/react-aws-cognito-passwordless-login/support"
Amplify.configure({
Auth: {
Cognito: {
userPoolId: process.env.NEXT_PUBLIC_AWS_COGNITO_USER_POOL_ID as string,
userPoolClientId: process.env.NEXT_PUBLIC_AWS_CLIENT_ID as string,
},
},
});
export default function Page() {
const [OTP, setOTP] = useState("");
const [username, setUsername] = useState("");
const [accountConfirmed, setAccountConfirmed] = useState(false);
const [accountCreated, setAccountCreated] = useState(false);
const [accountCreationFeedback, setAccountCreationFeedback] = useState("");
const [loading, setLoading] = useState(false);
const [accountConfirmedFeedback, setAccountConfirmedFeedback] = useState("");
const usernameChangeHandler = (
event: React.ChangeEvent<HTMLInputElement>
) => {
setUsername(event.target.value);
};
const OTPChangeHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
setOTP(event.target.value);
};
function handleKeyDown(event: React.KeyboardEvent) {
if (event.key === "Enter") {
createAccountClickHandler(event as unknown as React.MouseEvent);
}
}
function OTPKeyDownHandler(event: React.KeyboardEvent) {
if (event.key === "Enter") {
confirmAccountClickHandler(event as unknown as React.MouseEvent);
}
}
async function createAccountClickHandler(event: React.MouseEvent) {
event.preventDefault();
setAccountCreationFeedback("");
setAccountConfirmedFeedback("");
if (!username || !validateEmail(username)) {
setAccountCreationFeedback("Please enter a valid email address.");
return;
}
setLoading(true);
const password = generatePassword();
try {
await signUp({
username,
password,
options: {
userAttributes: {
email: username,
},
},
});
setAccountCreated(true);
setAccountCreationFeedback("");
} catch (error) {
if (error instanceof Error) {
setAccountCreationFeedback(error.message);
} else {
setAccountCreationFeedback("An unknown error occurred.");
}
}
setLoading(false);
}
async function confirmAccountClickHandler(event: React.MouseEvent) {
event.preventDefault();
if (!OTP) {
setAccountConfirmedFeedback("Please enter the confirmation code.");
return;
}
setLoading(true);
setAccountConfirmedFeedback("");
setAccountCreationFeedback("");
try {
const response = await confirmSignUp({ username, confirmationCode: OTP });
setAccountConfirmed(response.isSignUpComplete);
} catch (error) {
setAccountConfirmedFeedback("Error confirming account. Please try again.");
}
setLoading(false);
}
return (
<section style={{ fontSize: 24, maxWidth: 600, margin: "0 auto" }}>
<h1>Passworless Login</h1>
<div style={{ background: "#f2f2cd", padding: 20, margin: "20px 0" }}>
if you have an account already, please{" "}
<Link
href="/react-aws-cognito-passwordless-login/login"
style={{ textDecoration: "underline" }}
>
log in
</Link>
</div>
<h2>Step 1: Create Account</h2>
<small>Enter your email:</small>
<div>
<input
type="text"
onChange={usernameChangeHandler}
value={username}
style={{ width: "100%", fontSize: 24, margin: "12px 0", padding: 12 }}
tabIndex={1}
onKeyDown={handleKeyDown}
/>
<a
style={{
padding: 20,
border: "1px solid black",
cursor: "pointer",
display: "block",
textAlign: "center",
}}
onClick={createAccountClickHandler}
onKeyDown={handleKeyDown}
tabIndex={2}
>
Create Account
</a>
</div>
{accountCreated && (
<div style={{ background: "#f2f2cd", padding: 20, margin: "40px 0 0" }}>
Account created! Check your email for the confirmation code.
</div>
)}
{accountCreationFeedback && (
<div style={{ background: "#f9dfde", padding: 20, margin: "40px 0 0" }}>
{accountCreationFeedback}
</div>
)}
<div style={{ margin: "40px 0 0" }}>
<h2>Step 2: Confirm Account</h2>
<small>Enter the confirmation code you received on your email:</small>
<input
type="text"
onChange={OTPChangeHandler}
value={OTP}
style={{ width: "100%", fontSize: 24, margin: "12px 0", padding: 12 }}
tabIndex={3}
onKeyDown={OTPKeyDownHandler}
/>
<a
style={{
padding: 20,
border: "1px solid black",
cursor: "pointer",
display: "block",
textAlign: "center",
}}
onClick={confirmAccountClickHandler}
tabIndex={4}
onKeyDown={OTPKeyDownHandler}
>
Confirmed Account
</a>
</div>
{accountConfirmed && (
<div style={{ background: "#f2f2cd", padding: 20, margin: "40px 0 0" }}>
Account confirmed! You can now{" "}
<Link
href="/react-aws-cognito-passwordless-login/login"
style={{ textDecoration: "underline" }}
>
log in
</Link>
</div>
)}
{accountConfirmedFeedback && (
<div style={{ background: "#f9dfde", padding: 20, margin: "40px 0 0" }}>
{accountConfirmedFeedback}
</div>
)}
<div style={{ display: "flex", justifyContent: "center", marginTop: 40 }}>
{loading && <Loader />}
</div>
</section>
);
}