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>
This commit is contained in:
parent
64cb2ec56a
commit
d4b39251d9
8 changed files with 392 additions and 28 deletions
|
|
@ -5,18 +5,35 @@
|
|||
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.
|
||||
|
||||
The Google button is stubbed and disabled until the OAuth
|
||||
routes exist. It's here so the layout doesn't change when it
|
||||
starts working.
|
||||
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 { useState } from "react";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useLocation, useNavigate, useSearchParams } from "react-router-dom";
|
||||
|
||||
import { useAuth } from "../../lib/auth.tsx";
|
||||
import { ApiError } from "../../lib/api.ts";
|
||||
import { ApiError, get } from "../../lib/api.ts";
|
||||
|
||||
const GOOGLE_ENABLED = false;
|
||||
/* 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] " +
|
||||
|
|
@ -27,9 +44,21 @@ export default function AdminLogin() {
|
|||
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>(null);
|
||||
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";
|
||||
|
|
@ -60,7 +89,7 @@ export default function AdminLogin() {
|
|||
<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 read and triage site feedback.
|
||||
Sign in to edit the site.
|
||||
</p>
|
||||
|
||||
<form
|
||||
|
|
@ -68,6 +97,28 @@ export default function AdminLogin() {
|
|||
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]"
|
||||
|
|
@ -78,7 +129,7 @@ export default function AdminLogin() {
|
|||
id="admin-email"
|
||||
type="email"
|
||||
autoComplete="username"
|
||||
autoFocus
|
||||
autoFocus={!google}
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
className={`mt-2 ${fieldClass}`}
|
||||
|
|
@ -116,21 +167,6 @@ export default function AdminLogin() {
|
|||
{busy ? "Signing in…" : "Sign in"}
|
||||
</button>
|
||||
|
||||
{GOOGLE_ENABLED && (
|
||||
<>
|
||||
<div className="my-6 flex items-center gap-3 text-xs text-[#4a6b72]">
|
||||
<span className="h-px flex-1 bg-[#4a6b72]/20" />
|
||||
or
|
||||
<span className="h-px flex-1 bg-[#4a6b72]/20" />
|
||||
</div>
|
||||
<a
|
||||
href="/api/auth/google"
|
||||
className="block rounded-full border border-[#4a6b72]/30 px-6 py-3 text-center font-semibold text-[#26454c] transition-colors hover:bg-[#eef9fb]"
|
||||
>
|
||||
Continue with Google
|
||||
</a>
|
||||
</>
|
||||
)}
|
||||
</form>
|
||||
|
||||
<p className="mt-6 text-center text-xs text-[#4a6b72]">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue