Using a trigger with a not null value in the profile table #7317
-
I am using this function and trigger from the docs to automatically add new users to a profile table when they sign up https://supabase.com/docs/guides/auth/managing-user-data The catch is I have a role field in my profile table that is set to not allow null. The below code is giving the following error:
How do I add the user role? I am including the role at signup but I assume this won't work as the function triggered below does not have this data as it is copying from the auth.users which has no role const { user, error } = await client.auth.signUp({
email: "user@email.com",
password: "password",
role: 1,
}); -- inserts a row into public.users
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
insert into public.profiles (id)
values (new.id);
return new;
end;
$$;
-- trigger the function every time a user is created
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You have to pass any extra data in on signUp like this https://supabase.com/docs/reference/javascript/auth-signup#sign-up-with-additional-user-meta-data with the data object. The bigger problem is if you are doing this from a client the user can technically put in any role he wants into the raw_user_meta_data with .update later.... |
Beta Was this translation helpful? Give feedback.
You have to pass any extra data in on signUp like this https://supabase.com/docs/reference/javascript/auth-signup#sign-up-with-additional-user-meta-data with the data object.
Then in your trigger you have to use the new.raw_user_meta_data json column to get at your role data. You can search for examples in discussions on doing that.
The bigger problem is if you are doing this from a client the user can technically put in any role he wants into the raw_user_meta_data with .update later....