import { useState, useEffect } from 'react'; import { login, register } from '../api'; import { BACKEND_URL } from '../config'; // Minimal helper to start OAuth: fetch authorization_url and redirect async function startOauth(provider: 'mojeid' | 'bankid') { const base = BACKEND_URL.replace(/\/$/, ''); const url = `${base}/auth/${provider}/authorize`; try { const res = await fetch(url, { credentials: 'include' }); const data = await res.json(); if (data && typeof data.authorization_url === 'string') { window.location.assign(data.authorization_url); } else { alert('Cannot start OAuth.'); } } catch (e) { alert('Cannot start OAuth.'); } } export default function LoginRegisterPage({ onLoggedIn }: { onLoggedIn: () => void }) { const [mode, setMode] = useState<'login' | 'register'>('login'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(null); try { if (mode === 'login') { await login(email, password); onLoggedIn(); } else { await register(email, password, firstName || undefined, lastName || undefined); // After register, prompt login automatically await login(email, password); onLoggedIn(); } } catch (err: any) { setError(err?.message || 'Operation failed'); } finally { setLoading(false); } } // Add this useEffect hook useEffect(() => { // When the component mounts, add a class to the body document.body.classList.add('auth-page'); // When the component unmounts, remove the class return () => { document.body.classList.remove('auth-page'); }; }, []); // The empty array ensures this runs only once // The JSX no longer needs the wrapper div return (

{mode === 'login' ? 'Welcome back' : 'Create your account'}

setEmail(e.target.value)} />
setPassword(e.target.value)} />
{mode === 'register' && (
setFirstName(e.target.value)} />
setLastName(e.target.value)} />
)} {error &&
{error}
}
Or continue with
); }