Profile accounts: invites, any-Google sign-in, claiming People, visibility, Discord

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>
This commit is contained in:
Aidan Dona 2026-10-16 18:14:30 -05:00
parent 0cc2a3f721
commit e4c7e165d1
34 changed files with 2483 additions and 74 deletions

View file

@ -5,7 +5,9 @@
POST /api/auth/logout deletes the session row and cookie
GET /api/auth/me who am I, or 401
GET /api/auth/providers which ways in are switched on
GET /api/auth/google off to Google's account chooser
GET /api/auth/google off to Google's account chooser;
?for=account from the site's sign-in
page (any Google account, back there)
GET /api/auth/google/callback back from it: sign in, or
back to the login page with ?error=
@ -27,6 +29,11 @@
email, which then links it. A matching email already linked to a
different Google account is refused rather than relinked.
googleAuth.js checks the identity itself.
Profile accounts (076) are made by an admin's invite
(routes/profiles.js). One may sign in with a Google account
outside the Workspace, under the invited email; an outside
identity reaches nothing else. Signing in takes up the invite.
═══════════════════════════════════════════════════════════════ */
import { Hono } from "hono";
@ -153,7 +160,11 @@ const OAUTH_COOKIE_OPTIONS = {
const safeNext = (value) =>
typeof value === "string" && /^\/admin(\/[\w\-/]*)?$/.test(value) ? value : "/admin/home";
const backToLogin = (c, code) => c.redirect(`/admin/login?error=${encodeURIComponent(code)}`, 302);
// The page a sign-in started from: the admin's, or the site's
// (/sign-in, for profile accounts) when `from` is 'account'.
const SIGN_IN_PAGES = { admin: "/admin/login", account: "/sign-in" };
const backToLogin = (c, code, from = "admin") =>
c.redirect(`${SIGN_IN_PAGES[from] ?? SIGN_IN_PAGES.admin}?error=${encodeURIComponent(code)}`, 302);
auth.get("/providers", (c) =>
c.json({ password: true, google: Boolean(googleConfig()) }, 200, NO_STORE),
@ -162,17 +173,18 @@ auth.get("/providers", (c) =>
auth.get("/google", (c) => {
const config = googleConfig();
if (!config) return c.json({ error: "Google sign-in isn't set up." }, 404);
const from = c.req.query("for") === "account" ? "account" : "admin";
if (!config.redirectUri) {
// Said in the log, where whoever runs the box will look; the
// person at the login page just learns it didn't work.
console.error(
"google login: no return address. Set GOOGLE_REDIRECT_URI, or SITE_CONFIG to a site.json with a url.",
);
return backToLogin(c, "google_failed");
return backToLogin(c, "google_failed", from);
}
const { url, state, verifier } = startLogin(config);
setCookie(c, OAUTH_COOKIE, JSON.stringify({ state, verifier, next: safeNext(c.req.query("next")) }), {
const { url, state, verifier } = startLogin(config, { anyDomain: from === "account" });
setCookie(c, OAUTH_COOKIE, JSON.stringify({ state, verifier, from, next: safeNext(c.req.query("next")) }), {
...OAUTH_COOKIE_OPTIONS,
maxAge: 600,
});
@ -190,13 +202,15 @@ auth.get("/google/callback", async (c) => {
saved = null;
}
deleteCookie(c, OAUTH_COOKIE, OAUTH_COOKIE_OPTIONS);
const from = saved?.from === "account" ? "account" : "admin";
const back = (code) => backToLogin(c, code, from);
// Pressing Cancel on Google's screen comes back as ?error=.
if (c.req.query("error")) return backToLogin(c, "canceled");
if (c.req.query("error")) return back("canceled");
const code = c.req.query("code");
if (!saved?.state || !code || c.req.query("state") !== saved.state) {
return backToLogin(c, "expired");
return back("expired");
}
let identity;
@ -205,7 +219,7 @@ auth.get("/google/callback", async (c) => {
} catch (err) {
if (err instanceof GoogleLoginError) {
console.warn(`google login refused: ${err.code}`);
return backToLogin(c, err.code);
return back(err.code);
}
throw err;
}
@ -215,6 +229,20 @@ auth.get("/google/callback", async (c) => {
const byId = (id) => db.prepare(`SELECT ${columns} FROM admin_users WHERE id = ?`).get(id);
let user = db.prepare(`SELECT ${columns} FROM admin_users WHERE google_sub = ?`).get(identity.sub);
// From outside the Workspace, only a profile account: by its
// linked Google id, or its invited email.
if (!identity.inDomain) {
user ??= db.prepare(`SELECT ${columns} FROM admin_users WHERE email = ?`).get(identity.email);
if (!user || user.role !== "member") {
console.warn(`google login: ${identity.email} is outside the Workspace and has no profile account`);
return back(user ? "wrong_domain" : "no_account");
}
if (user.google_sub && user.google_sub !== identity.sub) {
console.warn(`google login: ${identity.email} is linked to another Google account`);
return back("linked_elsewhere");
}
}
// What the access groups give this person (accessGroups.js), asked
// only when it matters. Unanswerable is a refusal: a sign-in has to
// be shown to be allowed.
@ -232,14 +260,14 @@ auth.get("/google/callback", async (c) => {
if (!byEmail) {
if (!accessEnabled(db)) {
console.warn(`google login: no account for ${identity.email}`);
return backToLogin(c, "no_account");
return back("no_account");
}
// In an access group: the account is made now, Google only.
const found = await roleFromGroups();
if (found.failed) return backToLogin(c, "groups_unreachable");
if (found.failed) return back("groups_unreachable");
if (!found.role) {
console.warn(`google login: ${identity.email} is in no access group`);
return backToLogin(c, "not_in_group");
return back("not_in_group");
}
const id = db
.prepare(
@ -251,10 +279,10 @@ auth.get("/google/callback", async (c) => {
user = byId(id);
} else if (byEmail.google_sub) {
console.warn(`google login: ${identity.email} is linked to another Google account`);
return backToLogin(c, "linked_elsewhere");
return back("linked_elsewhere");
} else {
// Before linking: a refused sign-in mustn't change the account.
if (byEmail.is_active !== 1) return backToLogin(c, "disabled");
if (byEmail.is_active !== 1) return back("disabled");
db.prepare(`UPDATE admin_users SET google_sub = ? WHERE id = ?`).run(identity.sub, byEmail.id);
console.log(`google account linked to ${byEmail.email}`);
user = byEmail;
@ -266,23 +294,30 @@ auth.get("/google/callback", async (c) => {
// and with access groups switched off none gets in (it can't be
// rechecked, so it would keep its last role for good).
if (user.access_source === "group" && user.role !== "superadmin") {
if (!accessEnabled(db)) return backToLogin(c, "not_in_group");
if (user.is_active !== 1 && user.group_disabled !== 1) return backToLogin(c, "disabled");
if (!accessEnabled(db)) return back("not_in_group");
if (user.is_active !== 1 && user.group_disabled !== 1) return back("disabled");
const found = await roleFromGroups();
if (found.failed) return backToLogin(c, "groups_unreachable");
if (found.failed) return back("groups_unreachable");
applyGroupRole(db, user, found.role);
if (!found.role) return backToLogin(c, "not_in_group");
if (!found.role) return back("not_in_group");
user = byId(user.id);
}
if (user.is_active !== 1) return backToLogin(c, "disabled");
if (user.is_active !== 1) return back("disabled");
createSession(db, c, user.id);
// The invite is taken up: its link stops working.
db.prepare(
`UPDATE admin_users SET last_login_at = datetime('now'), name = COALESCE(name, ?) WHERE id = ?`,
).run(identity.name, user.id);
`UPDATE admin_users
SET last_login_at = datetime('now'), name = COALESCE(name, ?), google_sub = COALESCE(google_sub, ?),
invite_hash = NULL, invite_expires_at = NULL
WHERE id = ?`,
).run(identity.name, identity.sub, user.id);
console.log(`login ${user.email} (google)`);
// A profile account, or a sign-in from the site, goes to the
// profile page; the admin's goes back where it was headed.
if (user.role === "member" || from === "account") return c.redirect("/account", 302);
// Checked again: the cookie came back from the browser.
return c.redirect(safeNext(saved.next), 302);
});