/* ═══════════════════════════════════════════════════════════════ SIGN IN: /sign-in, for profile accounts (076) Accounts are by invite only (Data → Profile accounts), so there's no sign-up here: an invite's link (/account/welcome) sets the password, or the invited email signs in with Google, any Google account. The admin's own login (/admin/login) is the same accounts through another door; either lands an admin where they asked. A Google refusal comes back here as ?error= (routes/auth.js). ═══════════════════════════════════════════════════════════════ */ import { useEffect, useState } from 'react' import { Navigate, useNavigate, useSearchParams } from 'react-router-dom' import PageShell from '../components/PageShell.tsx' import GoogleLogo from '../components/GoogleLogo.tsx' import { useAuth } from '../lib/auth.tsx' import { ApiError, get } from '../lib/api.ts' import { BRAND } from '../lib/color.ts' export const SIGN_IN_TITLE = 'Sign in' const GOOGLE_ERRORS: Record = { canceled: 'Google sign-in was canceled.', expired: 'That sign-in took too long or was already used. Try again.', no_account: "There's no profile account for that Google account. Accounts are by invite: ask us to invite your email.", linked_elsewhere: "That email's account is linked to a different Google account.", disabled: 'That account has been turned off.', wrong_domain: 'Use your @nextgenerationofunity.org Google account for that one.', unverified: "That Google account's email isn't verified.", google_unreachable: "Couldn't reach Google. Try again in a moment.", google_failed: "Google sign-in didn't work. Try again, or use your password.", not_in_group: "That Google account isn't in a website access group.", groups_unreachable: "Couldn't check your Google groups just now. Try again in a moment.", } export const fieldClass = 'w-full border border-muted/50 bg-surface px-4 py-3 text-body outline-none transition-colors focus:border-brand focus:ring-2 focus:ring-brand/25' export const primaryButton = 'btn bg-action px-6 py-3 text-white transition-colors hover:bg-action-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-brand/40 disabled:cursor-not-allowed disabled:bg-muted/25' export function GoogleButton({ label = 'Sign in with Google' }: { label?: string }) { return ( {label} ) } /* Whether the server has Google sign-in switched on. */ export function useGoogleSignIn(): boolean { const [google, setGoogle] = useState(false) useEffect(() => { get('/auth/providers', { ttl: 0 }) .then((data) => setGoogle(Boolean(data.google))) .catch(() => setGoogle(false)) }, []) return google } export default function SignIn() { const { user, loading, login } = useAuth() const navigate = useNavigate() const [searchParams] = useSearchParams() const googleError = searchParams.get('error') const google = useGoogleSignIn() const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState( googleError ? (GOOGLE_ERRORS[googleError] ?? GOOGLE_ERRORS.google_failed) : null, ) const [busy, setBusy] = useState(false) if (!loading && user) return async function submit(event: React.FormEvent) { event.preventDefault() if (busy) return setBusy(true) setError(null) try { await login(email.trim(), password) navigate('/account', { replace: true }) } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't reach the server. Try again in a moment.") setPassword('') setBusy(false) } } return (
{google && ( <>
or with a password
)} setEmail(e.target.value)} className={`mt-2 ${fieldClass}`} /> setPassword(e.target.value)} className={`mt-2 ${fieldClass}`} /> {error && (

{error}

)} ), }, ]} /> ) }