NGU-Web/src/pages/admin/AdminLogin.tsx
Zaldimmar 80877a4659 Let Google Workspace groups grant admin access
Site settings → Access names a group for each of admin, editor and
viewer (migration 047). Someone in one who signs in with Google gets a
Google-only account with their highest group's role, and the role
follows the groups: at every sign-in, hourly while signed in, and on
Panel → Google groups → Re-sync now. Out of every group, the account
is disabled and signed out until they're back in one.

Group accounts' roles are locked in the Panel; a superadmin's Disable
sticks. Superadmins and admin-cli accounts are never changed. If
Google can't be asked, sign-ins are refused but the hourly check
leaves accounts alone. Panel → Check their groups tests an email.

Lookups use the service account through the Directory API as
GOOGLE_DIRECTORY_AS. Setup: docs/google-sign-in.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 17:44:04 -05:00

181 lines
7.1 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
ADMIN LOGIN
Deliberately outside PageShell and the site nav. This isn't a
page of the website; it's the door to the back office, and it
shouldn't carry a banner, a subnav, or a footer site map.
Google first, when the server has it set up (/auth/providers
says so; it's off until GOOGLE_CLIENT_ID and _SECRET are on the
service). It's a plain link: the server sends the browser to
Google and back, and a refusal comes back here as ?error=<code>,
worded below. Email and password stay underneath as the way in
if Google is ever unavailable.
═══════════════════════════════════════════════════════════════ */
import { useEffect, useState } from "react";
import { useLocation, useNavigate, useSearchParams } from "react-router-dom";
import { useAuth } from "../../lib/auth.tsx";
import { ApiError, get } from "../../lib/api.ts";
/* What a ?error= from the Google callback means, in the words the
person at the login page needs. */
const GOOGLE_ERRORS: Record<string, string> = {
cancelled: "Google sign-in was cancelled.",
expired: "That sign-in took too long or was already used. Try again.",
no_account:
"There's no admin account for that Google account. Ask a superadmin to add your email.",
linked_elsewhere:
"That email's admin account is linked to a different Google account.",
disabled: "That account has been disabled.",
wrong_domain: "Use your @nextgenerationofunity.org Google account.",
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. Ask a superadmin to add you to one.",
groups_unreachable: "Couldn't check your Google groups just now. Try again in a moment.",
};
const fieldClass =
"w-full rounded-xl 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 default function AdminLogin() {
const { login } = useAuth();
const navigate = useNavigate();
const location = useLocation();
const [searchParams] = useSearchParams();
const googleError = searchParams.get("error");
const [google, setGoogle] = useState(false);
useEffect(() => {
get("/auth/providers", { ttl: 0 })
.then((data) => setGoogle(Boolean(data.google)))
.catch(() => setGoogle(false));
}, []);
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState<any>(
googleError ? (GOOGLE_ERRORS[googleError] ?? GOOGLE_ERRORS.google_failed) : null,
);
const [busy, setBusy] = useState(false);
const destination = location.state?.from?.pathname ?? "/admin/home";
async function handleSubmit(event) {
event.preventDefault();
if (busy) return;
setBusy(true);
setError(null);
try {
await login(email.trim(), password);
navigate(destination, { replace: true });
} catch (err) {
setError(
err instanceof ApiError
? err.message
: "Couldn't reach the server. Try again in a moment.",
);
setPassword("");
setBusy(false);
}
}
return (
<div className="flex min-h-screen items-center justify-center bg-tint px-6 py-16">
<div className="w-full max-w-sm">
<h1 className="text-3xl font-bold text-link">NGU admin</h1>
<p className="mt-2 text-sm text-muted">
Sign in to edit the site.
</p>
<form
onSubmit={handleSubmit}
noValidate
className="mt-8 rounded-2xl border border-brand/20 bg-surface p-6"
>
{google && (
<>
<a
href={`/api/auth/google?next=${encodeURIComponent(destination)}`}
className="flex items-center justify-center gap-3 rounded-full border border-muted/30 px-6 py-3 font-semibold text-body transition-colors hover:bg-tint"
>
<svg viewBox="0 0 48 48" className="h-5 w-5" aria-hidden="true">
<path fill="#FFC107" d="M43.6 20.5H42V20H24v8h11.3C33.7 32.7 29.2 36 24 36c-6.6 0-12-5.4-12-12s5.4-12 12-12c3.1 0 5.8 1.2 7.9 3.1l5.7-5.7C34 6.1 29.3 4 24 4 12.9 4 4 12.9 4 24s8.9 20 20 20 20-8.9 20-20c0-1.3-.1-2.4-.4-3.5z" />
<path fill="#FF3D00" d="M6.3 14.7l6.6 4.8C14.7 15.1 19 12 24 12c3.1 0 5.8 1.2 7.9 3.1l5.7-5.7C34 6.1 29.3 4 24 4 16.3 4 9.7 8.3 6.3 14.7z" />
<path fill="#4CAF50" d="M24 44c5.2 0 9.9-2 13.4-5.2l-6.2-5.2C29.2 35.1 26.7 36 24 36c-5.2 0-9.6-3.3-11.3-8l-6.5 5C9.5 39.6 16.2 44 24 44z" />
<path fill="#1976D2" d="M43.6 20.5H42V20H24v8h11.3c-.8 2.2-2.2 4.2-4.1 5.6l6.2 5.2C37 39.2 44 34 44 24c0-1.3-.1-2.4-.4-3.5z" />
</svg>
Sign in with Google
</a>
<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="admin-email"
className="block text-sm font-medium text-body"
>
Email
</label>
<input
id="admin-email"
type="email"
autoComplete="username"
autoFocus={!google}
value={email}
onChange={(e) => setEmail(e.target.value)}
className={`mt-2 ${fieldClass}`}
/>
<label
htmlFor="admin-password"
className="mt-5 block text-sm font-medium text-body"
>
Password
</label>
<input
id="admin-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 rounded-xl 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 rounded-full bg-action px-6 py-3 font-semibold 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"
>
{busy ? "Signing in…" : "Sign in"}
</button>
</form>
<p className="mt-6 text-center text-xs text-muted">
Accounts are created on the server. Ask whoever runs the box.
</p>
</div>
</div>
);
}