Files
Fastchat/utils/authentication/register.ts
T

39 lines
1.1 KiB
TypeScript

"use server";
import { createClient } from "@/utils/supabase/server";
export type SignupResult =
| { success: true; emailConfirmationNeeded: true }
| { success: true; emailConfirmationNeeded: false }
| { error: { message: string } };
export async function signup(
email: string,
password: string,
): Promise<SignupResult> {
const supabase = await createClient();
const { data, error } = await supabase.auth.signUp({ email, password });
if (error) {
return {
error: { message: "An error occured when trying to create the account." },
};
}
if (
data.user?.identities?.length === undefined ||
data.user.identities.length < 1
) {
return { error: { message: "An account with this email already exists." } };
}
// The auth trigger (handle_new_auth_user) automatically inserts into
// public.users when a new auth.users row is created — no manual insert needed.
// If there's no session, email confirmation is required
const emailConfirmationNeeded = !data.session;
return { success: true, emailConfirmationNeeded };
}