NGU-Web/src/pages/admin/AdminLogin.tsx
Zaldimmar 048c7599ce Fix dark mode and bring the public design in line with the taste audit
- Dark mode: the countdown card, timeline section, Giving text, error
  text, the Leadership button, skeletons and the admin's dark text all
  follow the tokens; logos get a light plate in dark mode
- One BRAND accent (the button token, so the admin palette reaches
  every page) and one ACCENTS family in color.ts, replacing 20 local
  teal constants and the purple, ochre and rust section colours;
  neon green and gold become mint and brand green
- Shadows are tinted with the site's ink; glow shadows are gone
- One giving label, Give, in the nav, footer and Giving page
- Phosphor icons replace the hand-drawn SVGs and the glyph icons;
  Pathfinder paths pick a named icon (emoji still shown) and lose the
  watermark
- Home keeps two eyebrows; StatsBand's title is a real heading;
  section headings step down under the page title
- Nav links switch to the menu below lg, so they never wrap; jump
  links land clear of the header; press feedback on buttons
- Empty sections say what's coming and who to ask; loading is a
  skeleton; form placeholders and borders are readable; one
  corner-radius rule; theme-color for both modes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:54:03 -05:00

178 lines
6.9 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-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>
);
}