-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathsignIn.tsx
69 lines (59 loc) · 2.22 KB
/
signIn.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
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { useCustomAuth } from "./hooks/useCustomAuth"; // Assuming this hook is in this path
export const SignIn = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false);
const navigate = useNavigate();
const { startSignIn, status, errorCode, errorMessage } = useCustomAuth();
const handleSubmit = async (e) => {
e.preventDefault();
setIsSubmitting(true);
try {
const result = await startSignIn(username, password);
if (result === "SignIn.Completed") {
navigate("/success");
} else if (result === "SignIn.ChallengeRequired") {
navigate("/challenge");
}
} catch (error) {
console.error("Sign in error:", error);
} finally {
setIsSubmitting(false);
}
};
return (
<div className="sign-in-container">
<h2>Sign In</h2>
{errorMessage && (
<div className="error-message">{errorMessage}</div>
)}
<form onSubmit={handleSubmit}>
<div className="form-group">
<label htmlFor="username">Username</label>
<input
type="text"
id="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</div>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Signing in..." : "Sign In"}
</button>
</form>
</div>
);
};