Files
t5de-hub/apps/web/app/console/people/[id]/page.tsx
T
dhkatz f05874f800
Release / release (push) Successful in 5s
CI / test (push) Successful in 7m39s
CI / image (apps/api/Dockerfile, t5de-hub-api) (push) Successful in 32s
CI / image (apps/web/Dockerfile, t5de-hub-web) (push) Successful in 22s
feat(accounts): Discord sign-in and basic accounts (#22)
First half of docs/music.md's phase 1 (Accounts): Discord sign-in and basic accounts. The OIDC provider (Music as a trusted client, the `imvu` scope) is the next PR.

**Discord sign-in** (on when `DISCORD_CLIENT_ID` and `DISCORD_CLIENT_SECRET` are set; Discord redirects to `<API_URL>/api/auth/callback/discord`):

- The first time, it makes a **basic account** (`user.kind = "basic"`): Discord's name and email, no username or password. Basic accounts are for T5DE's other sites (Music); on the hub, every route but `/me` and `/account/finish` answers 403 `finish_setup`, and the website shows "Finish Setting Up Your Account" in place of every page.
- Only a **verified** Discord email (`user.validateUserInfo`): `email_not_verified`, or `email_not_found` for Discord accounts without one.
- **Never linked by matching email** (`accountLinking.disableImplicitLinking`): a Discord account whose email belongs to another T5DE account ends with `account_not_linked`, and the sign-in page says to sign in and link Discord from Account.
- Basic accounts don't count toward "the first account is an admin".

**Finishing setup**: `POST /api/v1/account/finish {username, password}` adds a password sign-in and a username in one transaction and makes the account a member (waiting for approval if registration is closed). Discord stays linked.

**Members** link Discord from the Account page (`/api/auth/link-social`; `account_already_linked_to_different_user` if it belongs to another account; merging basic accounts into members is phase 5) and unlink it (`DELETE /api/v1/account/discord`, audited). The console tags basic accounts "Discord only".

First half of docs/music.md's phase 1 (Accounts): Discord sign-in and basic accounts. The OIDC provider (Music as a trusted client, the `imvu` scope) is the next PR.

**Discord sign-in** (on when `DISCORD_CLIENT_ID` and `DISCORD_CLIENT_SECRET` are set; Discord redirects to `<API_URL>/api/auth/callback/discord`):

- The first time, it makes a **basic account** (`user.kind = "basic"`): Discord's name and email, no username or password. Basic accounts are for T5DE's other sites (Music); on the hub, every route but `/me` and `/account/finish` answers 403 `finish_setup`, and the website shows "Finish Setting Up Your Account" in place of every page.
- Only a **verified** Discord email (`user.validateUserInfo`): `email_not_verified`, or `email_not_found` for Discord accounts without one.
- **Never linked by matching email** (`accountLinking.disableImplicitLinking`): a Discord account whose email belongs to another T5DE account ends with `account_not_linked`, and the sign-in page says to sign in and link Discord from Account.
- Basic accounts don't count toward "the first account is an admin".

**Finishing setup**: `POST /api/v1/account/finish {username, password}` adds a password sign-in and a username in one transaction and makes the account a member (waiting for approval if registration is closed). Discord stays linked.

**Members** link Discord from the Account page (`/api/auth/link-social`; `account_already_linked_to_different_user` if it belongs to another account; merging basic accounts into members is phase 5) and unlink it (`DELETE /api/v1/account/discord`, audited). The console tags basic accounts "Discord only".

Also: Better Auth 1.7.14 → 1.7.26 (the OAuth provider package in the next PR needs it); migration `0021_account_kind` (one column, default `member`).

Tests: `apps/api/test/discord.test.ts` runs Better Auth's real social sign-in and callback against a stand-in for Discord's token and profile endpoints (basic account, verified email only, no linking by email, finishing setup, closed registration, linking and unlinking). Full suite 68/68, `check-types` and `build` pass locally. The new pages (sign-in's Discord button, Finish Setting Up, the Discord card) aren't checked in a browser yet: that needs the Discord application, then staging.

Before deploying: a Discord application with both redirect URLs (prod and staging), and its ID and secret in the hub stacks' secrets (dhkatz/infra).
Also: Better Auth 1.7.14 → 1.7.26 (the OAuth provider package in the next PR needs it); migration `0021_account_kind` (one column, default `member`).

Tests: `apps/api/test/discord.test.ts` runs Better Auth's real social sign-in and callback against a stand-in for Discord's token and profile endpoints (basic account, verified email only, no linking by email, finishing setup, closed registration, linking and unlinking). Full suite 68/68, `check-types` and `build` pass locally. The new pages (sign-in's Discord button, Finish Setting Up, the Discord card) aren't checked in a browser yet: that needs the Discord application, then staging.

Before deploying: a Discord application with both redirect URLs (prod and staging), and its ID and secret in the hub stacks' secrets (dhkatz/infra).

Co-authored-by: dhkatz <[email protected]>
2026-11-19 09:50:40 +00:00

217 lines
8.5 KiB
TypeScript

import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { ActionForm, SubmitButton } from "@/components/forms";
import { AgeTag, PageHeader, Tag } from "@/components/status";
import { DateTime } from "@/components/time";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { api, ApiError, unwrap } from "@/lib/api";
import { ban, decideAccount, setLinkCap, setRole, unban } from "../../actions";
import { ConsoleNav, requireModerator } from "../../console-nav";
export const metadata: Metadata = { title: "Person" };
export default async function PersonPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = await params;
const me = await requireModerator(`/console/people/${id}`);
let data;
try {
data = unwrap(await api.console.people({ id }).get());
} catch (e) {
if (e instanceof ApiError && e.status === 404) notFound();
throw e;
}
const { person, links, computers } = data;
return (
<>
<PageHeader title={person.username}>
<span className="flex flex-wrap items-center gap-2">
<span className="capitalize">{person.role}</span>
<span aria-hidden>·</span>
<span>
joined <DateTime iso={person.createdAt} dateOnly />
</span>
{person.banned && <Tag tone="danger">banned: {person.banReason}</Tag>}
{!person.approved && <Tag tone="strong">waiting for approval</Tag>}
{person.kind === "basic" && <Tag>Discord only: hasn't finished setting up</Tag>}
{person.twoFactor && <Tag>two-factor on</Tag>}
</span>
</PageHeader>
<ConsoleNav />
<div className="space-y-6">
{!person.approved && (
<Card>
<CardHeader>
<CardTitle>Waiting for Approval</CardTitle>
<CardDescription>
{person.email}. Until approved, they can only sign in. Declining deletes the account.
</CardDescription>
</CardHeader>
<CardContent>
<ActionForm action={decideAccount} fields={{ id: person.id }}>
<div className="flex gap-2">
<SubmitButton name="decision" value="approve" pendingLabel="Approving…">
Approve
</SubmitButton>
<SubmitButton
name="decision"
value="decline"
variant="outline"
pendingLabel="Declining…"
confirm={`Decline ${person.username}? Their account is deleted.`}
>
Decline
</SubmitButton>
</div>
</ActionForm>
</CardContent>
</Card>
)}
<Card>
<CardHeader>
<CardTitle>IMVU accounts</CardTitle>
<CardDescription>
{links.length} of {person.linkCap}. Only moderators see which IMVU accounts belong to one person.
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{links.length === 0 ? (
<p className="text-sm text-muted-foreground">None.</p>
) : (
<Table>
<TableHeader>
<TableRow>
<TableHead>IMVU account</TableHead>
<TableHead>ID</TableHead>
<TableHead>Linked</TableHead>
<TableHead>Age</TableHead>
<TableHead>Shows</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{links.map((l) => (
<TableRow key={l.imvuId}>
<TableCell className="font-medium">
<span className="flex items-center gap-2">
{l.username}
{l.online && (
<Tag tone="online" dot>
online
</Tag>
)}
</span>
</TableCell>
<TableCell className="text-muted-foreground tabular-nums">{l.imvuId}</TableCell>
<TableCell className="text-muted-foreground">
<DateTime iso={l.linkedAt} dateOnly />
</TableCell>
<TableCell>
<AgeTag link={l} />
</TableCell>
<TableCell className="text-muted-foreground">
{[l.showBadge && "badge", l.showPresence && "presence"].filter(Boolean).join(", ") || "nothing"}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)}
<p className="text-sm text-muted-foreground">
Signed in on {computers} computer{computers === 1 ? "" : "s"}.
</p>
</CardContent>
</Card>
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
<Card>
<CardHeader>
<CardTitle>Link cap</CardTitle>
<CardDescription>How many IMVU accounts they can link (normally 10).</CardDescription>
</CardHeader>
<CardContent>
<ActionForm action={setLinkCap} fields={{ id: person.id }}>
<div className="flex gap-2">
<Input
type="number"
name="linkCap"
defaultValue={person.linkCap}
min={0}
max={1000}
required
aria-label="Link cap"
className="w-28"
/>
<SubmitButton pendingLabel="Setting…">Set</SubmitButton>
</div>
</ActionForm>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle>Ban</CardTitle>
<CardDescription>
{person.banned
? `Banned: ${person.banReason}`
: "Covers all their IMVU accounts, and signs them out everywhere."}
</CardDescription>
</CardHeader>
<CardContent>
{person.banned ? (
<ActionForm action={unban} fields={{ id: person.id }}>
<SubmitButton variant="outline" pendingLabel="Unbanning…">
Unban
</SubmitButton>
</ActionForm>
) : (
<ActionForm action={ban} fields={{ id: person.id }}>
<Input
name="reason"
placeholder="Reason (they see it)"
required
maxLength={500}
aria-label="Reason"
/>
<SubmitButton
className="bg-red-600 text-white hover:bg-red-600/90 dark:bg-red-600 dark:hover:bg-red-600/90"
pendingLabel="Banning…"
confirm={`Ban ${person.username}?`}
>
Ban
</SubmitButton>
</ActionForm>
)}
</CardContent>
</Card>
{me.role === "admin" && (
<Card>
<CardHeader>
<CardTitle>Role</CardTitle>
<CardDescription>Moderators use the console; admins also set roles.</CardDescription>
</CardHeader>
<CardContent>
<ActionForm action={setRole} fields={{ id: person.id }}>
<div className="flex gap-2">
<NativeSelect name="role" defaultValue={person.role} aria-label="Role">
<NativeSelectOption value="user">User</NativeSelectOption>
<NativeSelectOption value="moderator">Moderator</NativeSelectOption>
<NativeSelectOption value="admin">Admin</NativeSelectOption>
</NativeSelect>
<SubmitButton pendingLabel="Setting…">Set</SubmitButton>
</div>
</ActionForm>
</CardContent>
</Card>
)}
</div>
</div>
</>
);
}