-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAuthentication.jsx
More file actions
135 lines (120 loc) · 3.94 KB
/
Authentication.jsx
File metadata and controls
135 lines (120 loc) · 3.94 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
import { useState } from "react";
import "./PdfViewer.css"; // Make sure this CSS file exists and is correctly styled
const hostURL = "https://localhost:44310/api/Authentication";
function Authentication({ onLogin }) {
const [isRegistering, setIsRegistering] = useState(false);
const [message, setMessage] = useState("");
const [username, setUsername] = useState("");
const validateEmail = (email) => /\S+@\S+\.\S+/.test(email);
const handleLogin = async (event) => {
event.preventDefault();
setMessage("");
const form = event.currentTarget;
const email = form.elements.email.value.trim();
const password = form.elements.password.value.trim();
if (!validateEmail(email)) {
setMessage("❌ Please enter a valid email.");
return;
}
try {
const response = await fetch(`${hostURL}/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
if (response.ok) {
const userData = await response.json();
localStorage.setItem("user", JSON.stringify(userData));
onLogin(userData);
} else {
setMessage("❌ Invalid email or password.");
}
} catch {
setMessage("⚠️ Server error during login.");
}
};
const handleRegister = async (e) => {
e.preventDefault();
setMessage("");
const form = e.currentTarget;
const email = form.elements.email.value;
const password = form.elements.password.value;
if (!validateEmail(email)) {
setMessage("❌ Please enter a valid email.");
return;
}
try {
const register = await fetch(`${hostURL}/register`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, email, password }),
});
const data = await register.json();
if (register.ok) {
setMessage("✅ Registration successful. You can now log in.");
setIsRegistering(false);
setUsername("");
} else {
setMessage(`❌ ${data.message || "Registration failed"}`);
}
} catch {
setMessage("⚠️ Server error during registration.");
}
};
return (
<div className="auth-container">
<h2 className="auth-title">{isRegistering ? "Register" : "Login"}</h2>
<form className="auth-form" onSubmit={isRegistering ? handleRegister : handleLogin}>
{isRegistering && (
<>
<label className="auth-label" htmlFor="username">Username:</label>
<input
id="username"
type="text"
name="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
className="auth-input"
placeholder="Your username"
/>
</>
)}
<label className="auth-label" htmlFor="email">Email:</label>
<input
id="email"
type="email"
name="email"
required
className="auth-input"
placeholder="you@example.com"
/>
<label className="auth-label" htmlFor="password">Password:</label>
<input
id="password"
type="password"
name="password"
required
className="auth-input"
placeholder="Your password"
/>
<button type="submit" className="auth-button primary">
{isRegistering ? "Register" : "Login"}
</button>
</form>
<button
className="auth-button secondary"
onClick={() => {
setIsRegistering(!isRegistering);
setMessage("");
setUsername("");
}}
style={{ marginTop: 12 }}
>
{isRegistering ? "Already have an account? Log in" : "Don't have an account? Register"}
</button>
{message && <p className="auth-message">{message}</p>}
</div>
);
}
export default Authentication;