Updated login and registration with email confirmation

This commit is contained in:
pascalyazid committed 2026-05-15 18:38:34 +02:00
1 parent 061bed2b59
commit 167baf9e82
5 files changed
+104 -22

No files matched your search

+26 -9
View File
@@ -1,10 +1,13 @@
"use server";
import { redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
import { signup } from "@/utils/authentication/register";
export type RegisterState = {
error?: string;
} | null;
export type RegisterState =
| { error?: string }
| { success: true; emailConfirmationNeeded: boolean }
| null;
export async function registerAction(
_prevState: RegisterState,
@@ -17,15 +20,29 @@ export async function registerAction(
return { error: "Email and password are required." };
}
let result;
try {
const result = await signup(email, password);
if (result?.error) {
return { error: result.error.message ?? "Registration failed." };
}
result = await signup(email, password);
} catch (error) {
const message = error instanceof Error ? error.message : "Registration failed.";
const message =
error instanceof Error
? error.message
: typeof error === "string"
? error
: "Registration failed.";
return { error: message };
}
return null;
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");
}