Why is my service role key client getting RLS errors or not returning data? #30146
Locked
GaryAustin1
announced in
Troubleshooting
Replies: 1 comment
-
|
After 1.5h of troubleshooting this finally made me understand why my seed script was violating RLS policy eventhough I used the service_role key. I was creating a user using .auth.signUp(), then tried to insert into another table using the same client. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a copy of a troubleshooting article on Supabase's docs site. It may be missing some details from the original. View the original article.
A Supabase client with the Authorization header set to the service role API key will ALWAYS bypass RLS. By default the Authorization header is the
apikeyused increateClient. If you are getting an RLS error then you have a user session getting into the client or you initialized with the publishable key. RLS in enforced based on theAuthorizationheader and not theapikeyheader.Three common cases of the
createClientapikeybeing replaced by a user session/token:SSR client initialized with service role. The SSR clients are designed to share the user session from cookies. The user session will override the default
apikeyfromcreateClientin theAuthorizationheader. If you are using SSR, always create a separate server client using supabase-js directly for service role.Edge functions or other server code setting the
Authorizationheader increateClientoptions directly to a user token/JWT. When you set theAuthorizationheader directly that overrides the default action of using theapikeyfor theAuthorizationheader.Server client initialized with service role using
signUpto create a user or other auth functions. Many auth functions will return a user session to the client making the call. When that happens theapikeywill be replaced by the user token/JWT in theAuthorizationheader. If you are wanting to create a user in a service role client useadmin.createUser()instead. Otherwise use a separate Supabase client for service role from other actions.Also note that adding
service_rolein RLS policies does nothing. Service role will never run the policies to begin with.Beta Was this translation helpful? Give feedback.
All reactions