-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathAuthForm.tsx
More file actions
122 lines (111 loc) · 2.84 KB
/
Copy pathAuthForm.tsx
File metadata and controls
122 lines (111 loc) · 2.84 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
import { useState } from "react";
import { authClient } from "../auth-client";
interface AuthFormProps {
onAuthSuccess: () => void;
}
export function AuthForm({ onAuthSuccess }: AuthFormProps) {
const [isLogin, setIsLogin] = useState(true);
const [email, setEmail] = useState("");
const [name, setName] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError("");
setLoading(true);
try {
if (isLogin) {
await authClient.signIn.email({
email,
password,
});
} else {
await authClient.signUp.email({
email,
name,
password,
});
}
onAuthSuccess();
} catch (err) {
setError(err instanceof Error ? err.message : "Authentication failed");
} finally {
setLoading(false);
}
};
return (
<div style={{ maxWidth: "400px", margin: "0 auto", padding: "20px" }}>
<h2>{isLogin ? "Sign In" : "Sign Up"}</h2>
<form onSubmit={handleSubmit} style={{ display: "flex", flexDirection: "column", gap: "15px" }}>
<div>
<label htmlFor="email">Email:</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
style={{ width: "100%", padding: "8px", marginTop: "5px" }}
/>
</div>
{!isLogin && (
<div>
<label htmlFor="name">Name:</label>
<input
id="name"
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
required
style={{ width: "100%", padding: "8px", marginTop: "5px" }}
/>
</div>
)}
<div>
<label htmlFor="password">Password:</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
style={{ width: "100%", padding: "8px", marginTop: "5px" }}
/>
</div>
{error && (
<div style={{ color: "red", fontSize: "14px" }}>{error}</div>
)}
<button
type="submit"
disabled={loading}
style={{
padding: "10px",
backgroundColor: "#007bff",
color: "white",
border: "none",
borderRadius: "4px",
cursor: loading ? "not-allowed" : "pointer"
}}
>
{loading ? "Loading..." : (isLogin ? "Sign In" : "Sign Up")}
</button>
</form>
<div style={{ textAlign: "center", marginTop: "15px" }}>
<button
type="button"
onClick={() => setIsLogin(!isLogin)}
style={{
background: "none",
border: "none",
color: "#007bff",
cursor: "pointer",
textDecoration: "underline"
}}
>
{isLogin ? "Need an account? Sign up" : "Already have an account? Sign in"}
</button>
</div>
</div>
);
}