https://supabase.com/ logo
How can I access values from auth.jwt() is a row-level policy?
g

Geoff

05/25/2023, 10:20 AM
Hi there, At sign-up I add a
tenant_id
field (which is an integer) to the new user's
app_metadata
, and I need to use this in row-level security policy. I can't figure out the syntax for accessing this in the policy statement. I thought it would be something like this:
SQL
BEGIN;
  ALTER POLICY "Enable tenant-based access" ON "public"."test" USING (tenant_id = (auth.jwt().app_metadata -> 'tenant_id'::integer));
COMMIT;
But I get a syntax error. Any ideas? Thanks!
s

silentworks

05/25/2023, 10:32 AM
I think
->
should be
->>
instead.
g

Geoff

05/25/2023, 10:41 AM
I tried this:
tenant_id = (auth.jwt().app_meta_data ->> 'tenant_id')::integer
and this:
tenant_id = ((auth.jwt() ->> 'app_meta_data')::jsonb ->> 'tenant_id')::integer
The first gives a syntax error, the second is accepted but doesn't work. 😕
Got it!
SQL
tenant_id = ((auth.jwt()->>'app_metadata')::jsonb->>'tenant_id')::integer
thanks @silentworks!
s

silentworks

05/25/2023, 10:56 AM
Happy to hear that.