// src/pages/LoginPage.tsx import { useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { useAuth } from '../context/AuthContext' import api from '../api/axios' import type { AuthResponse } from '../types' import axios from 'axios' const LoginPage = () => { // ── State ────────────────────────────────────────── const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [isLoading, setIsLoading] = useState(false) // ── Hooks ────────────────────────────────────────── const { login } = useAuth() // get login function from context const navigate = useNavigate() // programmatic navigation // ── Submit Handler ───────────────────────────────── const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError('') // clear previous errors setIsLoading(true) try { const response = await api.post('/auth/login', { email, password }) login(response.data) // save token + user to context navigate('/dashboard') // redirect to dashboard } catch (err: unknown) { // Axios wraps errors — need to extract message if (axios.isAxiosError(err)) { setError( err.response?.data?.message || 'Login failed' ) } else { setError('Something went wrong') } } finally { setIsLoading(false) } } // ── UI ───────────────────────────────────────────── return ( // Full screen centered layout
{/* Card container */}
{/* Header */}

Welcome back

Sign in to your account

{/* Error message */} {error && (
{error}
)} {/* Form */}
{/* Email field */}
setEmail(e.target.value)} placeholder="you@example.com" required className="w-full px-4 py-2.5 rounded-lg border border-gray-300 text-sm outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition" />
{/* Password field */}
setPassword(e.target.value)} placeholder="••••••••" required className="w-full px-4 py-2.5 rounded-lg border border-gray-300 text-sm outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition" />
{/* Submit button */}
{/* Register link */}

Don't have an account?{' '} Register

) } export default LoginPage