// Auth screens — login, signup, forgot password, reset, OTP. // All state lives in this component; on success it calls onAuthenticated(user). function AuthScreen({ initialMode = 'login', initialMessage, resetToken, verifyToken, onAuthenticated, onSkipToGuest, onShowLegal }) { const [mode, setMode] = useState(initialMode); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [name, setName] = useState(''); const [otpCode, setOtpCode] = useState(''); const [otpSent, setOtpSent] = useState(false); const [resetPw, setResetPw] = useState(''); const [busy, setBusy] = useState(false); const [err, setErr] = useState(''); const [info, setInfo] = useState(initialMessage || ''); // Handle email-link tokens (?verify=… / ?reset=…) useEffect(() => { if (verifyToken) { setBusy(true); window.apiClient.auth.verify(verifyToken) .then(res => { if (res.user) onAuthenticated(res.user); else { setMode('login'); setInfo('Email verified. You can now sign in.'); } }) .catch(e => setErr(e.message || 'Verification failed.')) .finally(() => setBusy(false)); } if (resetToken) { setMode('reset'); } // eslint-disable-next-line }, []); const submit = async (e) => { e?.preventDefault?.(); setErr(''); setInfo(''); setBusy(true); try { if (mode === 'login') { const res = await window.apiClient.auth.login(email, password); onAuthenticated(res.user); } else if (mode === 'signup') { await window.apiClient.auth.signup(email, password, name); setInfo('Check your email — we sent a link to verify your account.'); setMode('verify-pending'); } else if (mode === 'forgot') { await window.apiClient.auth.forgotPassword(email); setInfo('If an account exists for that email, a reset link has been sent.'); } else if (mode === 'reset') { await window.apiClient.auth.resetPassword(resetToken, resetPw); setInfo('Password reset. You can sign in with your new password.'); setMode('login'); } else if (mode === 'otp-request') { await window.apiClient.auth.otpRequest(email); setOtpSent(true); setInfo(`A 6-digit code has been emailed to ${email}.`); } else if (mode === 'otp-verify') { const res = await window.apiClient.auth.otpVerify(email, otpCode); onAuthenticated(res.user); } } catch (e) { setErr(e.message || 'Something went wrong.'); } finally { setBusy(false); } }; // Google sign-in (loaded lazily via global script tag) useEffect(() => { const clientId = window.__GOOGLE_CLIENT_ID; if (!clientId || mode !== 'login' && mode !== 'signup') return; const tryInit = () => { if (!window.google?.accounts?.id) return false; try { window.google.accounts.id.initialize({ client_id: clientId, callback: async (resp) => { if (!resp.credential) return; try { const r = await window.apiClient.auth.googleLogin(resp.credential); onAuthenticated(r.user); } catch (e) { setErr(e.message || 'Google sign-in failed.'); } }, }); const el = document.getElementById('googleBtn'); if (el) { el.innerHTML = ''; window.google.accounts.id.renderButton(el, { theme: 'outline', size: 'large', text: 'signin_with', width: 320 }); } return true; } catch { return false; } }; if (!tryInit()) { const t = setInterval(() => { if (tryInit()) clearInterval(t); }, 250); return () => clearInterval(t); } // eslint-disable-next-line }, [mode]); const titles = { login: ['Welcome back', 'Sign in to keep your progress synced.'], signup: ['Create your account', 'Free, forever. Sync across all devices.'], forgot: ['Forgot your password?', 'We\'ll send you a link to reset it.'], reset: ['Choose a new password', 'Pick something you\'ll remember.'], 'otp-request': ['Sign in with email code', 'No password needed — we\'ll email a one-time code.'], 'otp-verify': ['Enter the 6-digit code', 'It expires in 10 minutes.'], 'verify-pending': ['Almost done', 'Check your email and click the link.'], }; const [title, sub] = titles[mode] || titles.login; return (
{sub}
{err &&