Request with service_role key throws row level security #7586
Replies: 4 comments 1 reply
-
|
When you signin a user, the user’s jwt will be used as the authorization header not the default key. |
Beta Was this translation helpful? Give feedback.
-
|
Did you resolve this? @insomenia-youngbo I also have this issue |
Beta Was this translation helpful? Give feedback.
-
|
I fix this by add new SERVICE_ROLE_CLIENT: Without setting up cookies, the instance will use your key instead of JWT. |
Beta Was this translation helpful? Give feedback.
-
|
What's happening is that your The fix is to keep a dedicated server-only client for privileged work and never let it store or refresh a session: import { createClient } from '@supabase/supabase-js'
// server-side ONLY — never ship this key to the browser
export const supabaseAdmin = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_ROLE_KEY,
{ auth: { persistSession: false, autoRefreshToken: false } }
)Use I wrote up the full breakdown of this exact error here: https://www.guardlayer.io/blog/supabase-new-row-violates-rls-policy |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In backend api I tried to insert a data with service_role, but it throws rls error.
As I know service key bypasses rls. is it a bug or it bypasses in specific condition?
It works when I insert to other tables.
Beta Was this translation helpful? Give feedback.
All reactions