// components/LoginForm.tsx 'use client'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; export function LoginForm() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const router = useRouter() const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setIsLoading(true); console.log('🔵 [Form] Submitting login...'); try { const response = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, password }), }); console.log('🔵 [Form] Response status:', response.status); const data = await response.json(); if (!response.ok) { console.log('🔵 [Form] Login failed:', data.message); setError(data.message || 'Login failed'); setIsLoading(false); return; } console.log('🔵 [Form] Login successful!'); console.log('🔵 [Form] User data:', data); // Use window.location for a full page reload to ensure cookie is loaded console.log('🔵 [Form] Redirecting to /recruitment...'); // window.location.href = '/recruitment'; router.push("/recruitment") } catch (err) { console.error('🔵 [Form] Error:', err); setError('Something went wrong. Please try again.'); setIsLoading(false); } }; return (
); }