My current setup involves using supabase authentication for user sign up. Upon successful sign up, I am inserting user details into a "profiles" table. However, I am concerned about the scenario where a user successfully signs up with supabase auth but fails to get inserted into the profiles table. In such cases, I need to delete the account created in the auth system. Are there any recommended strategies or best practices to handle this situation effectively?
"use server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { createClient } from "@/utils/supabase/server";
export async function register(data: {
name: string;
email: string;
password: string;
}) {
const supabase = createClient();
// Auth Sign Up
const { data: authData, error: authError } = await supabase.auth.signUp({
email: data.email,
password: data.password,
options: {
data: { displayName: data.name },
},
});
if (authError) return { error: authError.message };
// Create new User Profile
const { error } = await supabase.from("profiles").insert({
id: authData.user?.id,
email: data.email,
username: data.name,
display_name: data.name,
});
if (error) {
// Remove user created in auth
// How do I do this?
return { error: error.message };
}
revalidatePath("/", "layout");
redirect("/messages");
}