create or replace function public.custom_access_token_hook(event jsonb)
returns jsonb
language plpgsql
as $$
declare
claims jsonb;
v_user_id uuid;
v_persona text;
begin
-- 1. Extract existing JWT claims
claims := event->'claims';
-- 2. Extract authenticated user id
v_user_id := (claims->>'sub')::uuid;
-- 3. Fetch persona from your table
select persona
into v_persona
from public.club
where user_id = v_user_id
limit 1;
-- 4. Inject persona into JWT if it exists
if v_persona is not null then
claims := jsonb_set(
claims,
'{persona}',
to_jsonb(v_persona),
true
);
end if;
-- 5. Put updated claims back into event
event := jsonb_set(event, '{claims}', claims, true);
-- 6. Return modified event
return event;
end;
$$;
-- Allow auth system to see public schema
grant usage on schema public
to supabase_auth_admin;
-- Allow hook execution
grant execute
on function public.custom_access_token_hook(jsonb)
to supabase_auth_admin;
grant all
on table public.club
to supabase_auth_admin;
hey there how to get persona inside user_metadata instead of getting the value in access_tokens