-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathpage.tsx
65 lines (60 loc) · 2.48 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
"use client";
import { useState } from "react";
import { useAuth } from "@/app/context/AuthContext";
import {
AuthFlowStateHandlerFactory,
SignUpAttributesRequired,
SignUpCompleted,
SignUpState,
UserAccountAttributes,
} from "@azure/msal-custom-auth";
import router from "next/router";
export default function AttributesPage() {
const { authState, setAuthState } = useAuth();
const [displayName, setDisplayName] = useState("");
const [message, setMessage] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const handleSubmitAttributes = async () => {
if (authState instanceof SignUpAttributesRequired) {
const attributes = new UserAccountAttributes();
attributes.setDisplayName(displayName);
const handler = AuthFlowStateHandlerFactory.create(authState);
const result = await handler.submitAttributes(attributes);
setAuthState(result.state);
if (result.state instanceof SignUpCompleted) {
setMessage("Sign-up successful!");
// Sample codes for sign-in after sign-up
// const signInHandler = AuthFlowStateHandlerFactory.create(result.state);
// const signInResult = await signInHandler.signIn();
} else if (result.state?.type === SignUpState.PasswordRequired) {
router.push("/sign-up/password");
} else if (result.state?.type === SignUpState.Failed) {
if (result.error?.isMissingRequiredAttributes()) {
setError("Required attributes are missing.");
}
if (result.error?.isAttributesValidationFailed()) {
setError("Attributes validation failed.");
} else {
setError(
result.error?.errorData?.message || "Unknown error."
);
}
}
}
};
return (
<main>
<div className="auth-container">
<h1>Enter Attributes</h1>
<input
type="input"
value={displayName}
onChange={(e) => setDisplayName(e.target.value)}
/>
<button onClick={handleSubmitAttributes}>Submit</button>
{error && <p style={{ color: "red" }}>{error}</p>}
{message && <p>{message}</p>}
</div>
</main>
);
}