| 1 | import { useState } from "react";
|
|---|
| 2 | import { useNavigate } from "react-router-dom";
|
|---|
| 3 | import { toast } from "react-toastify";
|
|---|
| 4 | import axiosInstance, { scheduleTokenRefresh } from "../api/axiosInstance";
|
|---|
| 5 | import { useAuth } from "../context/authContext";
|
|---|
| 6 | import type { User } from "../utils/types";
|
|---|
| 7 |
|
|---|
| 8 | const Login = () => {
|
|---|
| 9 | const [username, setUsername] = useState("");
|
|---|
| 10 | const [password, setPassword] = useState("");
|
|---|
| 11 | const [error, setError] = useState<string | null>(null);
|
|---|
| 12 | const { setUser } = useAuth();
|
|---|
| 13 | const navigate = useNavigate();
|
|---|
| 14 |
|
|---|
| 15 | const handleLogin = async (e: React.FormEvent) => {
|
|---|
| 16 | e.preventDefault();
|
|---|
| 17 | try {
|
|---|
| 18 | const response = await axiosInstance.post<{
|
|---|
| 19 | user: User;
|
|---|
| 20 | tokenExpiresIn: number;
|
|---|
| 21 | }>("/auth/login", { username, password });
|
|---|
| 22 | scheduleTokenRefresh(response.data.tokenExpiresIn);
|
|---|
| 23 | setUser(response.data.user);
|
|---|
| 24 | navigate("/");
|
|---|
| 25 | toast.success("Login successful!");
|
|---|
| 26 | } catch (error: any) {
|
|---|
| 27 | const errorMessage =
|
|---|
| 28 | error.response?.data?.error ||
|
|---|
| 29 | "Login failed. Please check your credentials.";
|
|---|
| 30 | setError(errorMessage);
|
|---|
| 31 | toast.error(errorMessage);
|
|---|
| 32 | }
|
|---|
| 33 | };
|
|---|
| 34 | return (
|
|---|
| 35 | <div className="flex flex-col items-center justify-center min-h-[90vh] bg-gray-100">
|
|---|
| 36 | <h2 className="text-2xl mb-4">Login</h2>
|
|---|
| 37 | <form
|
|---|
| 38 | onSubmit={handleLogin}
|
|---|
| 39 | className="bg-white p-6 rounded shadow-md w-80"
|
|---|
| 40 | >
|
|---|
| 41 | {error && <p className="text-red-500 mb-4">{error}</p>}
|
|---|
| 42 | <div className="mb-4">
|
|---|
| 43 | <label className="block text-gray-700 mb-2" htmlFor="username">
|
|---|
| 44 | Username
|
|---|
| 45 | </label>
|
|---|
| 46 | <input
|
|---|
| 47 | type="text"
|
|---|
| 48 | id="username"
|
|---|
| 49 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 50 | value={username}
|
|---|
| 51 | onChange={(e) => setUsername(e.target.value)}
|
|---|
| 52 | />
|
|---|
| 53 | </div>
|
|---|
| 54 | <div className="mb-4">
|
|---|
| 55 | <label className="block text-gray-700 mb-2" htmlFor="password">
|
|---|
| 56 | Password
|
|---|
| 57 | </label>
|
|---|
| 58 | <input
|
|---|
| 59 | type="password"
|
|---|
| 60 | id="password"
|
|---|
| 61 | className="w-full p-2 border border-gray-300 rounded"
|
|---|
| 62 | value={password}
|
|---|
| 63 | onChange={(e) => setPassword(e.target.value)}
|
|---|
| 64 | />
|
|---|
| 65 | </div>
|
|---|
| 66 | <button
|
|---|
| 67 | type="submit"
|
|---|
| 68 | className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg text-sm font-medium transition-colors duration-200 w-full cursor-pointer"
|
|---|
| 69 | >
|
|---|
| 70 | Login
|
|---|
| 71 | </button>
|
|---|
| 72 | </form>
|
|---|
| 73 | </div>
|
|---|
| 74 | );
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | export default Login;
|
|---|