Skip to content

Commit b926608

Browse files
author
Uttam Singh
committed
Updated Login.jsx with backend API integration and forgot password feature
1 parent d62fc89 commit b926608

File tree

1 file changed

+41
-39
lines changed

1 file changed

+41
-39
lines changed

frontend/src/pages/Login.jsx

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,57 @@ import logo from "../assets/logo.png";
33
import "./Login.css";
44

55
export default function Login() {
6-
const [username, setUsername] = useState("");
6+
const [email, setEmail] = useState("");
77
const [password, setPassword] = useState("");
88
const [loading, setLoading] = useState(false);
9-
10-
// ✅ TEMP ADMIN LOGIN (for testing)
11-
const ADMIN_ID = "[email protected]";
12-
const ADMIN_PASS = "Admin@123";
9+
const [error, setError] = useState("");
1310

1411
const handleSubmit = async (e) => {
1512
e.preventDefault();
1613
setLoading(true);
14+
setError("");
1715

18-
// ✅ Step 1: Local hardcoded admin check (for testing)
19-
if (username === ADMIN_ID && password === ADMIN_PASS) {
20-
alert("Admin login successful!");
21-
localStorage.setItem("role", "admin");
22-
window.location.href = "/dashboard";
23-
setLoading(false);
24-
return;
25-
}
26-
27-
// ✅ Step 2: (optional) Try backend login if not admin
2816
try {
2917
const response = await fetch(
30-
"https://fat-eibl-backend.onrender.com/users/login",
18+
"https://fat-eibl-backend-x1sp.onrender.com/users/login",
3119
{
3220
method: "POST",
33-
headers: { "Content-Type": "application/json" },
34-
body: JSON.stringify({ username, password }),
21+
headers: {
22+
Accept: "application/json",
23+
},
24+
body: new URLSearchParams({
25+
email: email,
26+
password: password,
27+
}),
3528
}
3629
);
3730

3831
const data = await response.json();
3932

40-
if (response.ok) {
41-
localStorage.setItem("token", data.access_token);
42-
localStorage.setItem("role", data.role);
33+
if (!response.ok) {
34+
setError(data.detail || "Invalid credentials");
35+
} else {
36+
alert(`Welcome ${data.user.name}!`);
37+
// Save user info to localStorage (for future dashboard use)
38+
localStorage.setItem("user", JSON.stringify(data.user));
4339

44-
if (data.role === "admin") {
45-
window.location.href = "/dashboard";
40+
// Redirect based on role
41+
if (data.user.role === "admin") {
42+
window.location.href = "/admin-dashboard";
4643
} else {
47-
window.location.href = "/user-dashboard";
44+
window.location.href = "/dashboard";
4845
}
49-
} else {
50-
alert(data.detail || "Invalid username or password");
5146
}
52-
} catch (error) {
53-
console.error("Login failed:", error);
54-
alert("Server error. Please try again later.");
47+
} catch (err) {
48+
setError("Unable to connect to server. Please try again later.");
5549
} finally {
5650
setLoading(false);
5751
}
5852
};
5953

6054
const handleForgotPassword = () => {
61-
alert("Forgot Password feature coming soon. Please contact Admin.");
55+
const mailtoLink = `mailto:[email protected]?subject=Forgot Password - FAT-EIBL&body=Dear Support,%0D%0A%0D%0AI forgot my password. Please help me reset it.%0D%0A%0D%0ARegistered Email: ${email}%0D%0A%0D%0AThank you.`;
56+
window.location.href = mailtoLink;
6257
};
6358

6459
return (
@@ -94,10 +89,10 @@ export default function Login() {
9489
}}
9590
>
9691
<input
97-
type="text"
98-
placeholder="Enter Username / Email"
99-
value={username}
100-
onChange={(e) => setUsername(e.target.value)}
92+
type="email"
93+
placeholder="Email"
94+
value={email}
95+
onChange={(e) => setEmail(e.target.value)}
10196
style={{
10297
width: "100%",
10398
padding: "10px",
@@ -109,31 +104,37 @@ export default function Login() {
109104
/>
110105
<input
111106
type="password"
112-
placeholder="Enter Password"
107+
placeholder="Password"
113108
value={password}
114109
onChange={(e) => setPassword(e.target.value)}
115110
style={{
116111
width: "100%",
117112
padding: "10px",
118-
marginBottom: "20px",
113+
marginBottom: "15px",
119114
border: "1px solid #ccc",
120115
borderRadius: "8px",
121116
}}
122117
required
123118
/>
124119

120+
{error && (
121+
<p style={{ color: "red", fontSize: "0.9rem", marginBottom: "10px" }}>
122+
{error}
123+
</p>
124+
)}
125+
125126
<button
126127
type="submit"
128+
disabled={loading}
127129
style={{
128-
backgroundColor: "#004aad",
130+
backgroundColor: loading ? "#7a9be6" : "#004aad",
129131
color: "white",
130132
border: "none",
131133
padding: "10px 20px",
132134
borderRadius: "8px",
133135
cursor: "pointer",
134136
width: "100%",
135137
}}
136-
disabled={loading}
137138
>
138139
{loading ? "Logging in..." : "Login"}
139140
</button>
@@ -142,8 +143,9 @@ export default function Login() {
142143
onClick={handleForgotPassword}
143144
style={{
144145
color: "#004aad",
145-
marginTop: "15px",
146146
cursor: "pointer",
147+
fontSize: "0.9rem",
148+
marginTop: "12px",
147149
textDecoration: "underline",
148150
}}
149151
>

0 commit comments

Comments
 (0)