Discord sign-in and basic accounts
Pull request / title (pull_request) Successful in 1s
CI / test (pull_request) Successful in 1m28s
CI / image (apps/api/Dockerfile, t5de-hub-api) (pull_request) Skipped
CI / image (apps/web/Dockerfile, t5de-hub-web) (pull_request) Skipped

Discord sign-in makes a basic account (Discord's verified email, no username
or password) for T5DE's other sites; it finishes setting up with a username
and password to use the hub. Implicit linking by email is off; members link
Discord from their account page. Better Auth 1.7.26.
This commit is contained in:
dhkatz committed 2026-11-19 01:11:51 -08:00
1 parent a66a3a4b6a
commit f0a9661f8e
24 files changed
+5214 -173

No files matched your search

+21
View File
@@ -52,6 +52,27 @@ export async function saveSettings(
});
}
/** Unlinks Discord (members keep their username and password). */
export async function unlinkDiscord(_: ActionState, _form: FormData): Promise<ActionState> {
return attempt(async () => {
unwrap(await api.account.discord.delete());
done();
});
}
/** A basic account (Discord only) becomes a member: a username and password. */
export async function finishSetup(_: ActionState, form: FormData): Promise<ActionState> {
// Passwords aren't trimmed.
const password = typeof form.get("password") === "string" ? (form.get("password") as string) : "";
if (password !== form.get("password2")) return { error: "The passwords don't match." };
const state = await attempt(async () => {
unwrap(await api.account.finish.post({ username: field(form, "username"), password }));
});
if (state?.error) return state;
revalidatePath("/", "layout");
redirect("/account?welcome=1");
}
export async function signOutComputers(_: ActionState, _form: FormData): Promise<ActionState> {
return attempt(async () => {
unwrap(await api.account["signout-computers"].post());
+36
View File
@@ -0,0 +1,36 @@
// Discord on a member's account: link it to sign in with Discord too (and on T5DE's other sites), or unlink it.
import { DiscordButton } from "@/components/discord-button";
import { ActionForm, ErrorText, SubmitButton } from "@/components/forms";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { unlinkDiscord } from "./actions";
export function DiscordCard({ linked, error }: { linked: boolean; error?: string }) {
return (
<Card>
<CardHeader>
<CardTitle>Discord</CardTitle>
<CardDescription>
{linked
? "Linked: you can sign in with Discord, here and on T5DE's other sites."
: "Link Discord to sign in with it, here and on T5DE's other sites."}
</CardDescription>
</CardHeader>
<CardContent className="space-y-3">
{error && <ErrorText>{error}</ErrorText>}
{linked ? (
<ActionForm action={unlinkDiscord}>
<SubmitButton
variant="outline"
pendingLabel="Unlinking…"
confirm="Unlink Discord? You'll sign in with your username and password only."
>
Unlink Discord
</SubmitButton>
</ActionForm>
) : (
<DiscordButton mode="link" />
)}
</CardContent>
</Card>
);
}
@@ -0,0 +1,31 @@
"use client";
// A basic account (made by signing in with Discord) chooses a username and password, and becomes a member.
import { ActionForm, SubmitButton } from "@/components/forms";
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { finishSetup } from "./actions";
export function FinishSetupForm() {
return (
<ActionForm action={finishSetup}>
<FieldGroup>
<Field>
<FieldLabel htmlFor="username">Username</FieldLabel>
<Input id="username" name="username" autoComplete="username" required minLength={3} maxLength={32} pattern="[A-Za-z0-9_]+" autoFocus />
<FieldDescription>Letters, digits and underscores. Other T5DE members see it.</FieldDescription>
</Field>
<Field>
<FieldLabel htmlFor="password">Password</FieldLabel>
<Input id="password" name="password" type="password" autoComplete="new-password" required minLength={10} maxLength={256} />
</Field>
<Field>
<FieldLabel htmlFor="password2">Password again</FieldLabel>
<Input id="password2" name="password2" type="password" autoComplete="new-password" required minLength={10} maxLength={256} />
</Field>
<Field>
<SubmitButton pendingLabel="Setting up…">Finish setting up</SubmitButton>
</Field>
</FieldGroup>
</ActionForm>
);
}
+5 -1
View File
@@ -7,16 +7,18 @@ import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { api, getMe, isModerator, unwrap } from "@/lib/api";
import { discordError } from "@/lib/discord";
import { claimCreator, linkAccount, signOutComputers } from "./actions";
import { AgeCard } from "./age-card";
import { ChallengeCard } from "./challenge-card";
import { DiscordCard } from "./discord-card";
import { LinkCard } from "./link-card";
import { TokensCard } from "./tokens-card";
import { TwoFactorCard } from "./two-factor-card";
export const metadata: Metadata = { title: "Account" };
export default async function Account({ searchParams }: { searchParams: Promise<{ welcome?: string; age?: string }> }) {
export default async function Account({ searchParams }: { searchParams: Promise<{ welcome?: string; age?: string; error?: string }> }) {
const me = await getMe();
if (!me) redirect("/signin?next=/account");
const [account, age, tokens] = await Promise.all([
@@ -120,6 +122,8 @@ export default async function Account({ searchParams }: { searchParams: Promise<
<AgeCard age={age} back={params.age === "back"} />
{account.discord.available && <DiscordCard linked={account.discord.linked} error={discordError(params.error)} />}
<Card>
<CardHeader>
<CardTitle>T5DE on your computers</CardTitle>