"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 { useEffect, useRef, useState } from "react"; import { ErrorText } from "@/components/forms"; import { Button } from "@/components/ui/button"; import { authClient } from "@/lib/auth-client"; /** auto: start at once (a T5DE site asked for Discord sign-in). */ export function DiscordButton({ mode, next = "/", auto = false }: { mode: "sign-in" | "link"; next?: string; auto?: boolean }) { 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); } } const started = useRef(false); useEffect(() => { if (!auto || started.current) return; started.current = true; void go(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [auto]); return (
{error && {error}}
); }