37 lines
1.9 KiB
TypeScript
37 lines
1.9 KiB
TypeScript
import { redirect } from "next/navigation";
|
|
import { eq } from "drizzle-orm";
|
|
import { getDb } from "@/db";
|
|
import { users } from "@/db/schema";
|
|
import { SiteHeader } from "@/components/public/site-header";
|
|
import { isAuthorizedAdmin } from "@/lib/auth/authorization";
|
|
import { getCustomerSession } from "@/lib/auth/server";
|
|
import { safeAuthReturnPath } from "@/lib/auth/return-path";
|
|
import { AccountForm } from "./account-form";
|
|
import { ProfileForm } from "./profile-form";
|
|
|
|
export async function AccountPage({ mode, next, verified, verificationError, setup = false }: {
|
|
mode: "login" | "register"; next: unknown; verified?: boolean; verificationError?: boolean; setup?: boolean;
|
|
}) {
|
|
const nextPath = safeAuthReturnPath(next);
|
|
const session = await getCustomerSession();
|
|
if (session) {
|
|
const returnPath = nextPath.startsWith("/admin") && !isAuthorizedAdmin(session.user) ? "/" : nextPath;
|
|
if (mode === "register" && setup) {
|
|
const [user] = await getDb().select({ name: users.name, email: users.email, image: users.image })
|
|
.from(users).where(eq(users.id, session.user.id)).limit(1);
|
|
if (!user) redirect("/auth/login");
|
|
return <div className="min-h-svh"><SiteHeader /><main className="grid min-h-[calc(100svh-4rem)] place-items-center p-4">
|
|
<div className="w-full max-w-sm"><ProfileForm {...user} nextPath={returnPath} setup /></div>
|
|
</main></div>;
|
|
}
|
|
if (returnPath !== nextPath) redirect(returnPath);
|
|
redirect(nextPath);
|
|
}
|
|
const siteKey = process.env.TURNSTILE_SITE_KEY ?? "";
|
|
if (process.env.NODE_ENV !== "development" && !siteKey) throw new Error("TURNSTILE_SITE_KEY is required");
|
|
return <div className="min-h-svh"><SiteHeader /><main className="grid min-h-[calc(100svh-4rem)] place-items-center p-4">
|
|
<AccountForm mode={mode} siteKey={siteKey} nextPath={nextPath}
|
|
verified={verified} verificationError={verificationError} />
|
|
</main></div>;
|
|
}
|