SupabaseS
Supabase6mo ago
Kieran

Custom JWT

Hi,
I've got a custom hook to update some data in my JWT. The logs say the hook is running, however, I don't see anything updated in the JWT payload?

I used the example one to cut down the JWT size and added some test claims in there and they don't appear in the payload?

create or replace function public.custom_access_token_hook(event jsonb)
returns jsonb
language plpgsql
as $$
declare
  original_claims jsonb;
  new_claims jsonb;
  claim text;
begin
  original_claims = event->'claims';
  new_claims = '{}'::jsonb;
  
  foreach claim in array array[
    -- add claims you want to keep here
    'iss',
    'aud',
    'exp',
    'iat',
    'sub',
    'role',
    'aal',
    'session_id',
    'email',
    'phone',
    'is_anonymous'
  ] loop
    if original_claims ? claim then
      -- original_claims contains one of the listed claims, set it on new_claims
      new_claims = jsonb_set(new_claims, array[claim], original_claims->claim);
    end if;
  end loop;
  
  -- Add custom test claims
  new_claims = jsonb_set(new_claims, array['test'], '"hook_working"');
  new_claims = jsonb_set(new_claims, array['admin'], 'true');
  new_claims = jsonb_set(new_claims, array['roles'], '["admin", "user"]');
  
  return jsonb_build_object('claims', new_claims);
end
$$;
Was this page helpful?