Discord sign-in and basic accounts
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:
1 parent
a66a3a4b6a
commit
f0a9661f8e
24 files changed
+5214
-173
No files matched your search
@@ -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());
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -36,6 +36,7 @@ export default async function PersonPage({ params }: { params: Promise<{ id: str
|
||||
</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>
|
||||
|
||||
@@ -76,7 +76,13 @@ export default async function People({
|
||||
<DateTime iso={p.createdAt} dateOnly />
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
{p.banned ? <Tag tone="danger">banned</Tag> : !p.approved && <Tag tone="strong">waiting</Tag>}
|
||||
{p.banned ? (
|
||||
<Tag tone="danger">banned</Tag>
|
||||
) : p.kind === "basic" ? (
|
||||
<Tag>Discord only</Tag>
|
||||
) : (
|
||||
!p.approved && <Tag tone="strong">waiting</Tag>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Header } from "@/components/header";
|
||||
import { NarrowCard } from "@/components/narrow-card";
|
||||
import { StagingBanner } from "@/components/staging-banner";
|
||||
import { getMe } from "@/lib/api";
|
||||
import { FinishSetupForm } from "./account/finish-setup-form";
|
||||
import { cn } from "@/lib/utils";
|
||||
import "./globals.css";
|
||||
|
||||
@@ -23,7 +24,14 @@ export default async function RootLayout({ children }: { children: React.ReactNo
|
||||
<StagingBanner />
|
||||
<Header />
|
||||
<main className="mx-auto w-full max-w-5xl flex-1 px-4 py-10 sm:px-6 sm:py-14">
|
||||
{me && !me.approved ? (
|
||||
{me?.kind === "basic" ? (
|
||||
<NarrowCard
|
||||
title="Finish Setting Up Your Account"
|
||||
description={`You're signed in with Discord as ${me.username}. Choose a username and password to use the hub; T5DE's other sites already work with your Discord sign-in.`}
|
||||
>
|
||||
<FinishSetupForm />
|
||||
</NarrowCard>
|
||||
) : me && !me.approved ? (
|
||||
<NarrowCard
|
||||
title="Waiting for Approval"
|
||||
description={`Thanks for signing up, ${me.username}. A moderator will approve your account soon; until then you can't use the hub.`}
|
||||
|
||||
@@ -1,18 +1,42 @@
|
||||
import type { Metadata } from "next";
|
||||
import { redirect } from "next/navigation";
|
||||
import { DiscordButton } from "@/components/discord-button";
|
||||
import { ErrorText } from "@/components/forms";
|
||||
import { NarrowCard } from "@/components/narrow-card";
|
||||
import { getMe } from "@/lib/api";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { api, getMe, unwrap } from "@/lib/api";
|
||||
import { discordError } from "@/lib/discord";
|
||||
import { safeNext } from "@/lib/paths";
|
||||
import { SignInForm } from "./signin-form";
|
||||
|
||||
export const metadata: Metadata = { title: "Sign in" };
|
||||
|
||||
export default async function SignIn({ searchParams }: { searchParams: Promise<{ next?: string | string[] }> }) {
|
||||
const next = safeNext((await searchParams).next);
|
||||
export default async function SignIn({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ next?: string | string[]; error?: string | string[] }>;
|
||||
}) {
|
||||
const params = await searchParams;
|
||||
const next = safeNext(params.next);
|
||||
if (await getMe()) redirect(next);
|
||||
const { discord } = unwrap(await api.hub.get());
|
||||
const error = discordError(params.error);
|
||||
return (
|
||||
<NarrowCard title="Sign in" description="to your T5DE account.">
|
||||
<SignInForm next={next} />
|
||||
<div className="space-y-6">
|
||||
{error && <ErrorText>{error}</ErrorText>}
|
||||
{discord && (
|
||||
<>
|
||||
<DiscordButton mode="sign-in" next={next} />
|
||||
<div className="flex items-center gap-3 text-xs text-muted-foreground">
|
||||
<Separator className="flex-1" />
|
||||
or with your username
|
||||
<Separator className="flex-1" />
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<SignInForm next={next} />
|
||||
</div>
|
||||
</NarrowCard>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// What went wrong with Discord sign-in or linking, as a sentence. Better Auth sends people back with ?error=<code>.
|
||||
const messages: Record<string, string> = {
|
||||
email_not_verified: "Your Discord account's email isn't verified. Verify it in Discord, then try again.",
|
||||
email_not_found: "Your Discord account has no email. Add one in Discord and verify it, then try again.",
|
||||
account_not_linked:
|
||||
"This email belongs to a T5DE account. Sign in to it with your username and password, then link Discord from your account page.",
|
||||
account_already_linked_to_different_user: "That Discord account already signs in to another T5DE account.",
|
||||
access_denied: "Discord sign-in was cancelled.",
|
||||
};
|
||||
|
||||
/** The sentence for an error Discord sign-in came back with (undefined if there's none). */
|
||||
export function discordError(code: string | string[] | undefined): string | undefined {
|
||||
if (typeof code !== "string" || !code) return undefined;
|
||||
return messages[code] ?? "Signing in with Discord didn't work. Try again.";
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
"@base-ui/react": "^1.9.0",
|
||||
"@elysiajs/eden": "1.4.9",
|
||||
"@t5de/api": "workspace:*",
|
||||
"better-auth": "1.7.14",
|
||||
"better-auth": "1.7.26",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"cn": "^0.5.0",
|
||||
"lucide-react": "^1.64.0",
|
||||
|
||||
Reference in new issue
Block a user