© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago
lopezjurip

Auth Hooks error

I can't figure out why this is failing with the following error:

https://example.com?error=server_error&error_description=500%3A+Error+invoking+access+token+hook.#error=server_error&error_description=500%253A+Error+invoking+access+token+hook.

Auth hook function:

create or replace function public.profiles_auth_hook(event jsonb)
returns jsonb
language plpgsql
as $$
declare
  claims jsonb;
  profile_id profiles.id%type;
begin
  claims := event->'claims';
  -- Check if 'user_metadata' exists in claims
  if jsonb_typeof(claims->'user_metadata') is null then
    -- If 'user_metadata' does not exist, create an empty object
    claims := jsonb_set(claims, '{user_metadata}', '{}');
  end if;
  -- get user
  select id into profile_id from profiles where profiles.user_id = (event->>'user_id')::uuid and profiles.disabled = false limit 1;
  claims := jsonb_set(claims, '{user_metadata,pid}', to_jsonb(profile_id::text));
  return jsonb_set(event, '{claims}', claims);
end;
$$;
grant execute on function public.profiles_auth_hook to supabase_auth_admin;
revoke execute on function public.profiles_auth_hook from authenticated, anon;
create or replace function public.profiles_auth_hook(event jsonb)
returns jsonb
language plpgsql
as $$
declare
  claims jsonb;
  profile_id profiles.id%type;
begin
  claims := event->'claims';
  -- Check if 'user_metadata' exists in claims
  if jsonb_typeof(claims->'user_metadata') is null then
    -- If 'user_metadata' does not exist, create an empty object
    claims := jsonb_set(claims, '{user_metadata}', '{}');
  end if;
  -- get user
  select id into profile_id from profiles where profiles.user_id = (event->>'user_id')::uuid and profiles.disabled = false limit 1;
  claims := jsonb_set(claims, '{user_metadata,pid}', to_jsonb(profile_id::text));
  return jsonb_set(event, '{claims}', claims);
end;
$$;
grant execute on function public.profiles_auth_hook to supabase_auth_admin;
revoke execute on function public.profiles_auth_hook from authenticated, anon;


Also, there is no documentation nor examples explaining how to use the new created claim in RLS policies.
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Use auth hooks
SupabaseSSupabase / help-and-questions
3y ago
Deploying custom auth hooks
SupabaseSSupabase / help-and-questions
8mo ago
Did Supabase Change Auth Hooks?
SupabaseSSupabase / help-and-questions
5mo ago
Auth Hooks on Free Plan
SupabaseSSupabase / help-and-questions
3y ago