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

@ -83,6 +83,20 @@ export const people = {
note: "Unpublished people are invisible everywhere.",
fields: [PUBLISHED_FIELD, LISTED_FIELD],
},
{
legend: "Hidden on the site",
note: "Ticked parts stay here but don't show anywhere on the site. Someone with a profile account who has claimed this person sets these on their profile too (Data → Profile accounts).",
fields: [
["photo", "Photo"],
["pronouns", "Pronouns"],
["tagline", "Tagline"],
["bio", "Bio"],
["email", "Public email"],
["phone", "Public phone"],
["location", "Location"],
["links", "Links"],
].map(([key, label]) => ({ path: `hide_${key}`, label: `Hide ${label.toLowerCase()}`, widget: "checkbox" })),
},
],
children: [

View file

@ -123,7 +123,7 @@ export function peek(path) {
return settled.get(path);
}
export function invalidate(path) {
export function invalidate(path?: string) {
if (path) {
cache.delete(path);
settled.delete(path);

View file

@ -10,13 +10,19 @@
RequireAuth guards routes. Worth being clear about what that
does and doesn't do: it hides the interface, not the data. The
protection is the 401 from /api/admin, this only stops someone
staring at an empty table.
staring at an empty table. A profile account (member) is signed
in but has no admin, so it's sent to its page on the site.
The provider wraps the whole app, since the site shows a profile
link and hidden pages by who's signed in. Signing in or out drops
the API cache: the nav and hidden pages depend on it.
═══════════════════════════════════════════════════════════════ */
import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { Navigate, Outlet, useLocation } from "react-router-dom";
import { get, post, ApiError } from "./api.ts";
import { get, invalidate, post, ApiError } from "./api.ts";
import { isStaff } from "./roles.ts";
const AuthContext = createContext<any>(null);
@ -45,6 +51,7 @@ export function AuthProvider({ children }) {
const login = useCallback(async (email, password) => {
const data = await post("/auth/login", { email, password });
invalidate();
setUser(data.user);
return data.user;
}, []);
@ -54,12 +61,19 @@ export function AuthProvider({ children }) {
await post("/auth/logout", {});
} finally {
// Whatever the server said, this browser is done.
invalidate();
setUser(null);
}
}, []);
// After a sign-in the server did itself (an invite's password).
const signedIn = useCallback((next) => {
invalidate();
setUser(next);
}, []);
return (
<AuthContext.Provider value={{ user, loading, login, logout }}>
<AuthContext.Provider value={{ user, loading, login, logout, signedIn }}>
{children}
</AuthContext.Provider>
);
@ -94,5 +108,7 @@ export function RequireAuth() {
return <Navigate to="/admin/login" state={{ from: location }} replace />;
}
if (!isStaff(user)) return <Navigate to="/account" replace />;
return <Outlet />;
}

View file

@ -14,18 +14,22 @@
role to be added, and there's now a fourth.
═══════════════════════════════════════════════════════════════ */
export const ROLES = ["viewer", "editor", "admin", "superadmin"] as const;
/* member is a profile account (076): the site and their own profile,
never the admin. */
export const ROLES = ["member", "viewer", "editor", "admin", "superadmin"] as const;
export type Role = (typeof ROLES)[number];
export const ROLE_RANK: Record<Role, number> = {
viewer: 1,
editor: 2,
admin: 3,
superadmin: 4,
member: 1,
viewer: 2,
editor: 3,
admin: 4,
superadmin: 5,
};
export const ROLE_LABELS: Record<Role, string> = {
member: "Profile",
viewer: "Viewer",
editor: "Editor",
admin: "Admin",
@ -35,9 +39,10 @@ export const ROLE_LABELS: Record<Role, string> = {
/* Each line is what that role adds to the one above it in the
list. Read top to bottom, they describe the whole ladder. */
export const ROLE_NOTES: Record<Role, string> = {
member: "Signs in to the site to edit their own profile. No admin.",
viewer: "Can read everything in the CMS and change nothing.",
editor: "Can create and update records. Can't delete anything.",
admin: "Can delete records, including feedback.",
admin: "Can delete records, including feedback. Invites profile accounts and approves their claims.",
superadmin: "Can manage accounts, roles and sessions.",
};
@ -55,3 +60,5 @@ export function atLeast(user: MaybeUser, role: Role): boolean {
export const canWrite = (user: MaybeUser) => atLeast(user, "editor");
export const canDelete = (user: MaybeUser) => atLeast(user, "admin");
export const isSuper = (user: MaybeUser) => atLeast(user, "superadmin");
/* Uses the admin at all: a profile account (member) doesn't. */
export const isStaff = (user: MaybeUser) => atLeast(user, "viewer");

44
src/lib/useAccount.ts Normal file
View file

@ -0,0 +1,44 @@
/* A profile account's own page (server/src/routes/account.js): the
account, the People record it has (as its owner edits it), its
claim, and Discord. Not cached: it changes as they edit it. */
import { useRecord, type Resource } from './useRecord.ts'
/* What an owner can take off the site (people.hide_*), in the order
the profile page lists them. */
export const HIDE_FIELDS = [
['photo', 'Photo'],
['pronouns', 'Pronouns'],
['tagline', 'Tagline'],
['bio', 'Bio'],
['email', 'Public email'],
['phone', 'Public phone'],
['location', 'Location'],
['links', 'Links'],
] as const
export type HideField = (typeof HIDE_FIELDS)[number][0]
export type MyPerson = {
id: string
display_name: string
is_published: boolean
photo: string | null
pronouns: string | null
tagline: string | null
bio: string | null
public_email: string | null
public_phone: string | null
location_label: string | null
hidden: Record<HideField, boolean>
}
export type MyAccount = {
user: { id: number; email: string; name: string | null; role: string }
person: MyPerson | null
claim: { id: number; person_id: string; display_name: string; status: 'pending' | 'approved' | 'declined' } | null
discord: { enabled: boolean; id: string | null; name: string | null }
}
export const useMyAccount = (enabled: boolean): Resource<MyAccount> =>
useRecord(enabled ? '/account' : null, 'account')