Migration 076 adds a member role below viewer (the site and its own profile, never the admin: /api/admin now needs viewer), invites, person claims, a Discord link on accounts and people.hide_* switches. Public reads of people go through v_people_public, which nulls hidden fields, and the views that read people are rebuilt over it. Admins invite an email from Data -> Profile accounts (link shown once, emailed on request). The invitee sets a password or signs in with any Google account under that email, asks to claim their People record, and once approved edits it and chooses what shows at /account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
6.3 KiB
TypeScript
157 lines
6.3 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
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=<code> (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<string, string> = {
|
|
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 (
|
|
<a
|
|
href="/api/auth/google?for=account"
|
|
className="flex items-center justify-center gap-3 btn border border-muted/30 px-6 py-3 text-body transition-colors hover:bg-tint"
|
|
>
|
|
<GoogleLogo />
|
|
{label}
|
|
</a>
|
|
)
|
|
}
|
|
|
|
/* 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<string | null>(
|
|
googleError ? (GOOGLE_ERRORS[googleError] ?? GOOGLE_ERRORS.google_failed) : null,
|
|
)
|
|
const [busy, setBusy] = useState(false)
|
|
|
|
if (!loading && user) return <Navigate to="/account" replace />
|
|
|
|
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 (
|
|
<PageShell
|
|
title={SIGN_IN_TITLE}
|
|
intro="For people with a profile account. Accounts are by invite: if you'd like one, ask us through the Request page."
|
|
sections={[
|
|
{
|
|
id: 'sign-in',
|
|
title: 'Your account',
|
|
accent: BRAND,
|
|
content: (
|
|
<div className="mx-auto max-w-md px-6">
|
|
<form onSubmit={submit} noValidate className="bg-raised p-6 shadow-md sm:p-8">
|
|
{google && (
|
|
<>
|
|
<GoogleButton />
|
|
<div className="my-6 flex items-center gap-3 text-xs text-muted">
|
|
<span className="h-px flex-1 bg-muted/20" />
|
|
or with a password
|
|
<span className="h-px flex-1 bg-muted/20" />
|
|
</div>
|
|
</>
|
|
)}
|
|
<label htmlFor="sign-in-email" className="block text-sm font-medium text-body">
|
|
Email
|
|
</label>
|
|
<input
|
|
id="sign-in-email"
|
|
type="email"
|
|
autoComplete="username"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
className={`mt-2 ${fieldClass}`}
|
|
/>
|
|
<label htmlFor="sign-in-password" className="mt-5 block text-sm font-medium text-body">
|
|
Password
|
|
</label>
|
|
<input
|
|
id="sign-in-password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
className={`mt-2 ${fieldClass}`}
|
|
/>
|
|
{error && (
|
|
<p role="alert" className="mt-5 border border-danger/30 bg-danger-tint px-4 py-3 text-sm text-danger">
|
|
{error}
|
|
</p>
|
|
)}
|
|
<button type="submit" disabled={busy || !email || !password} className={`mt-6 w-full ${primaryButton}`}>
|
|
{busy ? 'Signing in…' : 'Sign in'}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
)
|
|
}
|