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]>
This commit was merged in pull request #22.
This commit is contained in:
1 parent
a66a3a4b6a
commit
f05874f800
24 files changed
+5214
-173
No files matched your search
@@ -0,0 +1,40 @@
|
||||
"use client";
|
||||
// Discord sign-in (a basic account the first time, docs/music.md) and, from the account page, linking Discord to a
|
||||
// member's account. Discord sends people back to the API, which sends them on to these addresses on this site.
|
||||
import { useState } from "react";
|
||||
import { ErrorText } from "@/components/forms";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
|
||||
export function DiscordButton({ mode, next = "/" }: { mode: "sign-in" | "link"; next?: string }) {
|
||||
const [pending, setPending] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function go() {
|
||||
setPending(true);
|
||||
setError("");
|
||||
const origin = window.location.origin;
|
||||
const back =
|
||||
mode === "link"
|
||||
? { callbackURL: `${origin}/account`, errorCallbackURL: `${origin}/account` }
|
||||
: { callbackURL: origin + next, errorCallbackURL: `${origin}/signin?next=${encodeURIComponent(next)}` };
|
||||
const { error } =
|
||||
mode === "link"
|
||||
? await authClient.linkSocial({ provider: "discord", ...back })
|
||||
: await authClient.signIn.social({ provider: "discord", ...back });
|
||||
// On success the browser is already on its way to Discord.
|
||||
if (error) {
|
||||
setError(error.message ?? "That didn't work. Try again.");
|
||||
setPending(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<Button type="button" variant={mode === "link" ? "outline" : "default"} className={mode === "link" ? undefined : "w-full"} disabled={pending} onClick={() => void go()}>
|
||||
{pending ? "Going to Discord…" : mode === "link" ? "Link Discord" : "Sign in with Discord"}
|
||||
</Button>
|
||||
{error && <ErrorText>{error}</ErrorText>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user