"use server"; import { redirect } from "next/navigation"; import { revalidatePath } from "next/cache"; import { signup } from "@/utils/authentication/register"; export type RegisterState = | { error?: string } | { success: true; emailConfirmationNeeded: boolean } | null; export async function registerAction( _prevState: RegisterState, formData: FormData, ): Promise { const email = formData.get("email"); const password = formData.get("password"); if (typeof email !== "string" || typeof password !== "string") { return { error: "Email and password are required." }; } let result; try { result = await signup(email, password); } catch (error) { const message = error instanceof Error ? error.message : typeof error === "string" ? error : "Registration failed."; return { error: message }; } if ("error" in result) { return { error: result.error.message ?? "Registration failed." }; } if (result.emailConfirmationNeeded) { return { success: true, emailConfirmationNeeded: true }; } // Auto-confirmed — redirect to profile // redirect() must be outside try/catch because it throws to halt execution revalidatePath("/", "layout"); redirect("/profile"); }