50 lines
2.2 KiB
TypeScript
50 lines
2.2 KiB
TypeScript
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 { Separator } from "@/components/ui/separator";
|
|
import { api, getMe, unwrap } from "@/lib/api";
|
|
import { discordError } from "@/lib/discord";
|
|
import { continueURL, requestedProvider, requestingClient } from "@/lib/oauth";
|
|
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<Record<string, string | string[] | undefined>> }) {
|
|
const params = await searchParams;
|
|
// Opened for another T5DE site (Music): back to it once signed in.
|
|
const forSite = continueURL(params);
|
|
const next = forSite ?? safeNext(params.next);
|
|
if (await getMe()) redirect(next);
|
|
const { discord } = unwrap(await api.hub.get());
|
|
const clientId = requestingClient(params);
|
|
const site = forSite && clientId ? (await api.oauth.clients({ clientId }).get()).data : undefined;
|
|
const siteName = site && "client" in site ? site.client.name : undefined;
|
|
const error = discordError(params.error);
|
|
// The site asked for Discord (its "Sign in with Discord" button): go straight there, unless that just failed.
|
|
const autoDiscord = discord && forSite !== undefined && requestedProvider(params) === "discord" && !error;
|
|
return (
|
|
<NarrowCard
|
|
title="Sign in"
|
|
description={siteName ? `with your T5DE account to continue to ${siteName}.` : "to your T5DE account."}
|
|
>
|
|
<div className="space-y-6">
|
|
{error && <ErrorText>{error}</ErrorText>}
|
|
{discord && (
|
|
<>
|
|
<DiscordButton mode="sign-in" next={next} auto={autoDiscord} />
|
|
<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>
|
|
);
|
|
}
|