Auth / supabase_auth_admin permissions: best practice for deleting user? #28776
-
Not sure if this has changed, but we're running into some unexpected behavior with the deleteUser API. we have this set up with an edge function that authenticates the user, and then uses the service role key to all call admin.deleteUser(). the main issue is that we have a number of cascade deletes and triggers that fire after a user gets deleted, which cause delete user to fail unexpectedly. you can reproduce this pretty trivially with: a profiles table that has a foreign key referencing auth.users that cascade deletes
an items table with a profile_id column that references the profiles table and cascade deletes
a trigger on the items table that updates a total_item_count table with the number of saves per item.
the first error we encountered was that we actually had to specify the schema on the trigger. I think this is because the auth function is being called from the auth schema. As written above, we got an error on user deletion: we were able to correct that by modifying the trigger to be the second error I'm less certain how to resolve is that even with the correction, we hit a permissions error: this is all taking place from should would love to get some suggestions here for best practices – and for quicker reference you can also reproduce this by simply deleting a user from the dashboard as it seems to use the same APIs. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Beta Was this translation helpful? Give feedback.
-
got it, thank you! you're right that the User Management guide does use a security definer function for triggers, which I clearly overlooked. I think I missed that because this trigger was not on the perhaps this is worth a quick mention in the Postgres Triggers guide to just point out that any triggers potentially cascaded by
|
Beta Was this translation helpful? Give feedback.
supabase_auth_admin is a "weak" role. It does not even have RLS bypass. It is not granted to public. Which is why the user management guides all show using a security definer function for triggers.
edit: Even in auth-hooks they show having to grant it access to a table as in the case of the auth-hooks function it is not security definer: