import { t } from '../i18n' import { LanguageSelect } from '../i18n/react' import { Eye, EyeOff, Loader2 } from 'lucide-react' import { useEffect, useState, type FormEvent, type ReactNode } from 'react' import { PASSWORD_MIN, requestPasswordReset, signIn, signInWithGoogle, signOut, signUp, updatePassword, verifyTotp } from '../lib/cloud/auth' import { USERNAME_RE } from '../lib/cloud/social' import { cx } from '../lib/utils' import { RANDOM_USERNAME } from './social/WelcomeProfile' type Mode = 'login' | 'signup' | 'forgot' function PasswordField({ value, onChange, placeholder, autoComplete }: { value: string; onChange: (v: string) => void; placeholder: string; autoComplete: string }) { const [show, setShow] = useState(false) return (
onChange(e.target.value)} placeholder={placeholder} autoComplete={autoComplete} minLength={PASSWORD_MIN} maxLength={72} required />
) } function Shell({ children, subtitle }: { children: ReactNode; subtitle: string }) { return (

Bobinoo

{subtitle}

{children}
) } /** Écran de connexion / inscription (affiché quand les comptes sont activés et qu'on n'est pas connecté). */ export default function AuthScreen({ notice, onExplore, initialMode = 'login' }: { notice?: string; /** Mode découverte : regarder l'app sans compte */ onExplore?: () => void; initialMode?: 'login' | 'signup' }) { const [mode, setMode] = useState(initialMode) const [email, setEmail] = useState('') const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [confirm, setConfirm] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState() const [info, setInfo] = useState(notice) const cleanUsername = username.trim().replace(/^@/, '').toLowerCase() const usernameOk = USERNAME_RE.test(cleanUsername) && !RANDOM_USERNAME.test(cleanUsername) // Retour arrière depuis Google (page restaurée du cache) : les boutons ne restent pas bloqués useEffect(() => { const onShow = (e: PageTransitionEvent) => { if (e.persisted) setBusy(false) } window.addEventListener('pageshow', onShow) return () => window.removeEventListener('pageshow', onShow) }, []) const switchTo = (m: Mode) => { setMode(m) setError(undefined) setInfo(undefined) } const google = async () => { setError(undefined) setInfo(undefined) setBusy(true) try { await signInWithGoogle() } catch (err) { setError((err as Error).message) setBusy(false) } } const submit = async (e: FormEvent) => { e.preventDefault() setError(undefined) setInfo(undefined) if (mode === 'signup' && !usernameOk) return setError(t('social.usernameRule')) if (mode === 'signup' && password !== confirm) return setError(t('auth.mismatch')) setBusy(true) try { if (mode === 'login') await signIn(email, password) else if (mode === 'signup') { const needsConfirm = await signUp(email, password, cleanUsername) if (needsConfirm) { setInfo(t('auth.created', { email: email.trim() })) setMode('login') setPassword('') setConfirm('') } } else { await requestPasswordReset(email) setInfo(t('auth.resetSent')) setMode('login') } } catch (err) { setError((err as Error).message) } finally { setBusy(false) } } return ( {mode !== 'forgot' && (
{(['login', 'signup'] as const).map((m) => ( ))}
)} {mode !== 'forgot' && ( <>
{t('auth.or')}
)}
{mode === 'forgot' &&

{t('auth.forgotTitle')}

} {mode === 'signup' && (
@ setUsername(e.target.value.toLowerCase())} placeholder={t('welcome.usernamePh')} aria-label={t('social.username')} autoComplete="username" autoCapitalize="off" spellCheck={false} maxLength={21} required />

{t('social.usernameRule')}

)} setEmail(e.target.value)} placeholder={t('auth.email')} autoComplete="email" inputMode="email" autoCapitalize="off" spellCheck={false} maxLength={320} required /> {mode !== 'forgot' && ( )} {mode === 'signup' && ( <>

{t('auth.minChars', { n: PASSWORD_MIN })}

)} {error && (

{error}

)} {info && (

{info}

)} {mode === 'signup' &&

{t('auth.consent')}

} {onExplore && mode !== 'forgot' && ( )}
{mode === 'login' && ( )} {mode === 'forgot' && ( )}

{t('auth.privacy')}

) } /** Choix d'un nouveau mot de passe (après le lien « mot de passe oublié »). */ export function NewPasswordScreen({ onDone }: { onDone: () => void }) { const [password, setPassword] = useState('') const [confirm, setConfirm] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState() const submit = async (e: FormEvent) => { e.preventDefault() if (password !== confirm) return setError(t('auth.mismatch')) setBusy(true) setError(undefined) try { await updatePassword(password) onDone() } catch (err) { setError((err as Error).message) } finally { setBusy(false) } } return (

{t('auth.minChars', { n: PASSWORD_MIN })}

{error && (

{error}

)}
) } /** Champ « code à 6 chiffres » (application d'authentification). */ export function TotpField({ value, onChange, autoFocus }: { value: string; onChange: (v: string) => void; autoFocus?: boolean }) { return ( onChange(e.target.value.replace(/[^0-9 ]/g, ''))} placeholder={t('mfa.code')} aria-label={t('mfa.code')} autoFocus={autoFocus} required /> ) } /** Connexion d'un compte protégé par la double authentification : code de l'application d'authentification. */ export function MfaScreen({ factorId }: { factorId: string }) { const [code, setCode] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState() const submit = async (e: FormEvent) => { e.preventDefault() if (busy) return setBusy(true) setError(undefined) try { await verifyTotp(factorId, code) } catch (err) { setError((err as Error).message) setCode('') } finally { setBusy(false) } } return (

{t('mfa.loginText')}

{error && (

{error}

)}
) } /** Liens vers les pages légales (statiques, hors de l'app). */ export function LegalLinks() { return ( ) }