NGU-Web/src/pages/admin/AdminLogin.tsx
Zaldimmar d4b39251d9 Add Google sign-in for @nextgenerationofunity.org admin accounts
The login page offers "Sign in with Google" above email and password,
once GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are set on the service.
It's the server-side authorization-code flow with PKCE; the ID token
comes straight from Google's token endpoint and is checked for
issuer, audience, expiry, a verified email and the Workspace domain
(Google's hd claim).

Only accounts admin-cli has made get in. The first Google sign-in
links the account with the same email; later ones match by the link.
A disabled account, one linked to another Google account, or a
missing account is refused, with a message on the login page, and a
refused sign-in never changes the account. admin-cli list shows which
accounts are linked. Setup is in docs/google-sign-in.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 21:27:12 -05:00

178 lines
7 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.",
};
const fieldClass =
"w-full rounded-xl border border-[#4a6b72]/25 bg-white px-4 py-3 text-[#26454c] " +
"outline-none transition-colors focus:border-[#138ba0] focus:ring-2 focus:ring-[#138ba0]/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-[#eef9fb] px-6 py-16">
<div className="w-full max-w-sm">
<h1 className="text-3xl font-bold text-[#138ba0]">NGU admin</h1>
<p className="mt-2 text-sm text-[#4a6b72]">
Sign in to edit the site.
</p>
<form
onSubmit={handleSubmit}
noValidate
className="mt-8 rounded-2xl border border-[#138ba0]/20 bg-white p-6"
>
{google && (
<>
<a
href={`/api/auth/google?next=${encodeURIComponent(destination)}`}
className="flex items-center justify-center gap-3 rounded-full border border-[#4a6b72]/30 px-6 py-3 font-semibold text-[#26454c] transition-colors hover:bg-[#eef9fb]"
>
<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-[#4a6b72]">
<span className="h-px flex-1 bg-[#4a6b72]/20" />
or with a password
<span className="h-px flex-1 bg-[#4a6b72]/20" />
</div>
</>
)}
<label
htmlFor="admin-email"
className="block text-sm font-medium text-[#26454c]"
>
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-[#26454c]"
>
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-[#b3261e]/30 bg-[#fdf3f2] px-4 py-3 text-sm text-[#b3261e]"
>
{error}
</p>
)}
<button
type="submit"
disabled={busy || !email || !password}
className="mt-6 w-full rounded-full bg-[#138ba0] px-6 py-3 font-semibold text-white transition-colors hover:bg-[#0f7183] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#138ba0]/40 disabled:cursor-not-allowed disabled:bg-[#4a6b72]/25"
>
{busy ? "Signing in…" : "Sign in"}
</button>
</form>
<p className="mt-6 text-center text-xs text-[#4a6b72]">
Accounts are created on the server. Ask whoever runs the box.
</p>
</div>
</div>
);
}