Skip to content

Commit 5909371

Browse files
committed
Add ability to login
1 parent f93c936 commit 5909371

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

frontend/src/app/components/auth/LoginComponent.tsx

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,56 @@ import { Label } from "@/components/ui/label";
1414
import { Eye, EyeOff } from "lucide-react";
1515
import Link from "next/link";
1616
import { useState } from "react";
17+
import { useRouter } from "next/navigation";
18+
import { login } from "@/services/userServiceApi";
19+
import { handleApiError, handleApiSuccess } from "@/services/errorHandler";
20+
import { toast } from "sonner";
21+
import { addToken } from "@/services/userServiceCookies";
1722

1823
export default function LoginForm() {
1924
const [email, setEmail] = useState("");
2025
const [password, setPassword] = useState("");
2126
const [showPassword, setShowPassword] = useState(false);
27+
28+
const router = useRouter();
2229

23-
function handleLogin() {
24-
window.location.href = "/home";
25-
}
30+
const handleLogin = async (e: React.MouseEvent<HTMLButtonElement>) => {
31+
e.preventDefault();
32+
33+
// Basic validation
34+
if (!email || !password) {
35+
toast.error("Please fill in all fields");
36+
return;
37+
}
38+
39+
try {
40+
const response = await login(email, password);
41+
42+
// Check if we got a token
43+
const token = response?.data?.data?.accessToken || response?.data?.accessToken;
44+
if (!token) {
45+
toast.error("Login failed: No token received");
46+
return;
47+
}
48+
49+
// Store token and show success
50+
addToken(token);
51+
handleApiSuccess(
52+
"Login successful!",
53+
`Welcome back! Redirecting to homepage...`,
54+
response.data
55+
);
56+
57+
// Redirect after short delay
58+
setTimeout(() => {
59+
router.push("/home");
60+
}, 1000);
61+
62+
} catch (error) {
63+
console.error('Login error details:', error);
64+
handleApiError(error, "Login failed");
65+
}
66+
};
2667

2768
return (
2869
<Card className="min-h-[50%] min-w-[40%]">
@@ -72,7 +113,7 @@ export default function LoginForm() {
72113
</div>
73114

74115
<div className="flex justify-center ">
75-
<Button onClick={() => handleLogin()} className="w-full">
116+
<Button onClick={handleLogin} className="w-full">
76117
Login
77118
</Button>
78119
</div>

0 commit comments

Comments
 (0)