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.
41 lines
1.7 KiB
TypeScript
41 lines
1.7 KiB
TypeScript
"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>
|
|
);
|
|
}
|