// src/pages/Login.tsx import { useEffect, useState } from 'react'; interface LoginProps { onLoginSuccess: () => void; } export default function Login({ onLoginSuccess }: LoginProps) { const [status, setStatus] = useState<'loading' | 'success' | 'error'>('loading'); const [errorMessage, setErrorMessage] = useState(''); useEffect(() => { const urlParams = new URLSearchParams(window.location.search); const token = urlParams.get('token'); if (!token) { setStatus('error'); setErrorMessage('No token provided in URL'); return; } // Call backend login endpoint fetch('http://localhost:8080/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', // IMPORTANT: This allows cookies to be set body: JSON.stringify({ token }), }) .then(async (res) => { if (res.ok) { setStatus('success'); // Remove token from URL for security window.history.replaceState({}, document.title, '/'); // Wait a moment then notify parent setTimeout(() => { onLoginSuccess(); }, 1000); } else { const error = await res.text(); setStatus('error'); setErrorMessage(error || 'Login failed'); } }) .catch((err) => { setStatus('error'); setErrorMessage(err.message); }); }, [onLoginSuccess]); return (
Please wait while we authenticate you.
> )} {status === 'success' && ( <>Redirecting to the app...
> )} {status === 'error' && ( <>{errorMessage}
> )}