Question

RLS blocks my own inserts from the app but the dashboard works

Solved · 17 views · asked by sam_builds · edited

I have a table orders with RLS on. Reading works. Inserting from the app gives the error below, but in the Supabase table editor the insert works fine.

new row violates row-level security policy for table "orders"

The policy Claude Code wrote:

create policy "insert own" on orders for insert with check (auth.uid() = user_id);
What I’ve tried
  1. Enabled the policy Claude Code wrote (it says using (true) on select)
  2. Logged out and in again
  3. Checked that auth.uid() is not null in a select
Comment

2 answers

Marked as helpful by the asker
mira_dev · edited

The dashboard runs as postgres, which bypasses RLS, so it will always work there. That's why it feels inconsistent.

Your policy is fine. The usual cause is that the insert doesn't include user_id, so auth.uid() = null is false. Check what your app actually sends:

await supabase.from('orders').insert({ ...order, user_id: user.id })

Better: give the column a default so the client can't get it wrong:

alter table orders alter column user_id set default auth.uid();
Comment
lena_ops · edited

Also make sure you are not inserting with the service role key from the browser. If it "works" that way, you have a much bigger problem than a policy.

Comment