Database Trigger
Is possible to create a database trigger wich can access the auth object of supabase? Or something like this...
9 Replies
Basically, I need to create a trigger to automaticaly insert a value in a column based on wich user is making the insert on database
just reference auth.uid() where you need the user id
This is not working for me
Function when called directly by the user, works fine using auth.uid()
but it seems like when is on a trigger, doesnot works
aut.uid() returns null
@garyaustin what do you think?
It will work in a trigger if the original call was from the API, or at least it should.
in the paper it makes sense to work
auth.uid() looks at the jwt value that was sent in,
okay
i will do more tests
maybe im missing something here
already tried a lot of things but
seems to make sense this works
Maybe show your code. It for sure works in triggers I have done that. And I think I have done it in security definer functions also. Since it looks right at the jwt value (stored as a setting) it is not impacted by service role.
it could also be null if you're using the service_role secret to initialize the api call. at the top of your trigger you could just coalesce the auth.uid() with a default one you want on service role calls. otherwise you can handle the logic completely separately for these calls as below