S
Supabase•3mo ago
WiseDolphin

RLS Violation

Hey guys, I just started using supabase and I am having some trouble. I created a form where users can sign up and it adds them to the authentication users table but I created a Users table in public which should store some additional information. I added RLS to it but whenever I try creating an account this gets printed: new row violates row-level security policy for table "Users". I have added photos of the policies I am using for the Users table, any help would be really appericated.
No description
No description
49 Replies
garyaustin
garyaustin•3mo ago
How are you adding data to the table? Do you have a signed in user session when you make the call? You can use auth.getSession to check or I believe the API Gateway log has an authentication section that shows what user is making the request.
WiseDolphin
WiseDolphinOP•2mo ago
Sorry I am very new to supabase and this whole stuff, what do you mean exactly
garyaustin
garyaustin•2mo ago
How are you adding data to your Users table?
WiseDolphin
WiseDolphinOP•2mo ago
for the insert, here is my code const handleSubmit = async (e: React.FormEvent) => { e.preventDefault();
if (!validateForm()) { return; } setIsLoading(true); try { const { data: authData, error: authError} = await supabase.auth.signUp({ email, password, }); if (authError) throw authError; if (!authData.user) throw new Error('User creation failed'); const {error: dbError } = await supabase.from('Users').insert({ id: authData.user.id, username: username, name: name, }); if (dbError) throw dbError; setSubmitMessage('Account created successfully! Please check your email to verify your account.'); setEmail(''); setPassword(''); setName(''); setUsername(''); } catch (error: any) { console.error('Signup error:', error); setSubmitMessage(error.message || 'An error occurred during signup. Please try again.'); } finally { setIsLoading(false); } };
garyaustin
garyaustin•2mo ago
Are you requiring the user to confirm their email?
WiseDolphin
WiseDolphinOP•2mo ago
I disabled that feature as I wanted to make sure its working rn
garyaustin
garyaustin•2mo ago
The code you are showing will not work in "real life" as signup does not return a user session if you want to confirm their email is real, which you do. It might work for testing if you have that off as then signup does return a session. Go to the dashboard logs session and API Gateway log and look for /rest/v1/Users You should see an error on it. Then look in the details and scroll down until you see an authorization section.
WiseDolphin
WiseDolphinOP•2mo ago
Ok let me see
WiseDolphin
WiseDolphinOP•2mo ago
Ya it saws its invalid
No description
WiseDolphin
WiseDolphinOP•2mo ago
Okay what would be the alternative to this then? regarding the code or are you just saying that email vertification needs to be on in real life (which I understand)
garyaustin
garyaustin•2mo ago
That does say you have an authenticated user. The normal way is using this process: https://supabase.com/docs/guides/auth/managing-user-data That sets up a trigger on auth.users when a new user is created and you pass your data in as part of signUp data option. It populates your public table for you.
WiseDolphin
WiseDolphinOP•2mo ago
When looking at my table the Users table in public
No description
WiseDolphin
WiseDolphinOP•2mo ago
it is designed like this shouldnt this get the uid from auth as well? Also I would like to add that it does create a user in the auth table, just has an error in the Users table So what is wrong in my policies then?
garyaustin
garyaustin•2mo ago
Ahh... You used restrictive. You really should never use that. Permissive is the default and simpler method.
WiseDolphin
WiseDolphinOP•2mo ago
Oh could you tell me the difference between using permissive and restrictive cus isnt restrictive more secure? also is that the case for all policies?
garyaustin
garyaustin•2mo ago
Always use permissive. That means you are allowing a user that meets the policy to have access. No policies and no access. Restrictive means you are blocking a specific user or type of user. But it also requires first a permissive policy that it then "subtracts" from.
WiseDolphin
WiseDolphinOP•2mo ago
Ohhhhhhhh Okay I get
garyaustin
garyaustin•2mo ago
I don't even look at that part of a policy when helping people as it is so rarely used and the issue is usually something else.
WiseDolphin
WiseDolphinOP•2mo ago
Ya 😂 Thank you for all your help man
Tariq
Tariq•2mo ago
Ideally youd want to use a trigger function so Supabase automatically populates the Users table for you whenever a new user is created in auth
WiseDolphin
WiseDolphinOP•2mo ago
ah okay thank you
WiseDolphin
WiseDolphinOP•2mo ago
Yo I tried to make the trigger function but it does not populate my Users table.
No description
No description
No description
WiseDolphin
WiseDolphinOP•2mo ago
Do you know what the issue is? or @garyaustin if you know
Tariq
Tariq•2mo ago
I think youre watching the wrong table is the "Users" table part of Auth or is that the custom table you yourself made?
garyaustin
garyaustin•2mo ago
Tariq is correct you are putting your trigger on Users and not auth.users as the guide suggests. Also you really should not use capital letters for names of things in Postgres. Where you have public.Users in your code has to be public."Users" because of that. All tables or columns with capitals have to be in double quotes.
Tariq
Tariq•2mo ago
Do they let you watch the auth.users table? Because I cant find it in the select field
No description
Tariq
Tariq•2mo ago
When i did it I wrote the trigger manually with the sql editor
garyaustin
garyaustin•2mo ago
You add the trigger thru SQL. Sort of a minimal test of should you be adding something to a reserved schema I guess.
Tariq
Tariq•2mo ago
i see
WiseDolphin
WiseDolphinOP•2mo ago
Okay I understand, would I also create the function in SQL or is the trigger neough enough
WiseDolphin
WiseDolphinOP•2mo ago
No description
WiseDolphin
WiseDolphinOP•2mo ago
like should this work? or do I have to connect it to something as well
Tariq
Tariq•2mo ago
possibly, im not an expert on sql to be honest with you the only difference with mine is that i wrote "procedure" instead of "function" and "public.create_new_user()" instead of "create_new_user()"
create trigger on_auth_user_created
after insert on auth.users
for each row
execute procedure public.handle_new_user();
create trigger on_auth_user_created
after insert on auth.users
for each row
execute procedure public.handle_new_user();
garyaustin
garyaustin•2mo ago
Your trigger is fine. Make sure you remove the trigger on Users.
WiseDolphin
WiseDolphinOP•2mo ago
Yup I did let me test it out
Tariq
Tariq•2mo ago
you might have to Run it first for it to take effect, im not sure
garyaustin
garyaustin•2mo ago
Run what?
Tariq
Tariq•2mo ago
the query
garyaustin
garyaustin•2mo ago
Yes for sure.
WiseDolphin
WiseDolphinOP•2mo ago
oh I forgot the on hahahaa okay it worked thought thank you guys
Tariq
Tariq•2mo ago
np
WiseDolphin
WiseDolphinOP•2mo ago
this stuff kinda confusing no or just me hahaha
Tariq
Tariq•2mo ago
make sure you remove the code where you add data to the Users table since you wont need it anymore
garyaustin
garyaustin•2mo ago
If you get errors signing up, you will need to check the Postgres logs for help debugging your trigger function. For instance if you forgot security definer type for the function or left it as public.Users without "Users". The log will give the specific issue versus just 500 DB error.
WiseDolphin
WiseDolphinOP•2mo ago
Ah okay hey guys have you guys created your projects with next.js Because I want to get the session right on different components but I am not sure how to exactly do that Should I create one general file which gets the session and then in each component get the session or wrap the layout with a session 'container', but that would cause the whole application to use client
Tariq
Tariq•2mo ago
Wrapping a server component with a client component doesn't turn it into a client component The children between the client component would still be server components But regardless ur not meant to do it that way, no
Tariq
Tariq•2mo ago
You'd want to follow this guide first
WiseDolphin
WiseDolphinOP•2mo ago
Okay thanks I will look into this

Did you find this page helpful?