// 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 (
V
IELTS Vocab Coach
For Bangla-speaking learners

{title}

{sub}

{err &&
{err}
} {info &&
{info}
} {(mode === 'login' || mode === 'signup' || mode === 'forgot' || mode === 'otp-request') && (
{mode === 'signup' && (
setName(e.target.value)} placeholder="Your name" autoFocus />
)}
setEmail(e.target.value)} placeholder="you@example.com" autoFocus={mode !== 'signup'} />
{(mode === 'login' || mode === 'signup') && (
setPassword(e.target.value)} placeholder={mode === 'signup' ? 'At least 8 characters' : '••••••••'} /> {mode === 'signup' && password.length > 0 && password.length < 8 && (
At least 8 characters required.
)}
)} {mode === 'signup' && (
By creating an account, you agree that your writing and speech submissions are sent to third-party AI services (Google Gemini, Groq) for feedback and scoring. See our{' '} .
)}
)} {mode === 'otp-request' && otpSent && ( )} {mode === 'otp-verify' && (
setEmail(e.target.value)} disabled />
setOtpCode(e.target.value.replace(/\D/g, ''))} placeholder="123456" autoFocus style={{ fontFamily: 'monospace', fontSize: 18, letterSpacing: '0.4em', textAlign: 'center' }} />
)} {mode === 'reset' && (
setResetPw(e.target.value)} placeholder="At least 8 characters" autoFocus />
)} {mode === 'verify-pending' && (
We've sent a verification link to {email}. Click it to finish signing up. The link expires in 24 hours.
)} {/* Alternative auth methods */} {(mode === 'login' || mode === 'signup') && ( <>
or
)} {/* Mode switcher */}
{mode === 'login' && ( <> · )} {mode === 'signup' && ( )} {(mode === 'forgot' || mode === 'reset' || mode === 'otp-request' || mode === 'otp-verify') && ( )}
{/* Guest mode */} {onSkipToGuest && (mode === 'login' || mode === 'signup') && (
Progress saves to your browser only.
)}
); } Object.assign(window, { AuthScreen });