Repository navigation
Expand file tree
/
Copy pathsupabase_auth_hook.sql
More file actions
74 lines (63 loc) · 2.23 KB
/
Copy pathsupabase_auth_hook.sql
File metadata and controls
74 lines (63 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
BEGIN;
-- Supabase custom access token hook.
--
-- Keep this hook as a coarse application-membership gate.
-- Authorization itself is enforced by the Nuxt/Prisma/CASL RBAC layer and by RBAC-aware Storage RLS.
--
-- This deliberately does not serialize application roles or permissions into the JWT.
-- Permission changes should be read from the database, not from stale tokens.
CREATE OR REPLACE FUNCTION public.custom_access_token_hook(event jsonb)
RETURNS jsonb
LANGUAGE plpgsql
AS
$$
DECLARE
app_user_id text;
has_admin_profile boolean;
has_participant_profile boolean;
BEGIN
-- Resolve the application user from JWT sub
SELECT id
INTO app_user_id
FROM public."User"
WHERE "supabaseAuthId" = (event -> 'claims' ->> 'sub');
-- Deny tokens for Auth users that are not known by the application
-- This should not happen.
IF app_user_id IS NULL THEN
RETURN jsonb_build_object(
'error',
jsonb_build_object(
'http_code', 403,
'message', 'User not found.'
)
);
END IF;
-- A user must have exactly one profile (Admin or Participant)
SELECT EXISTS (SELECT 1 FROM public."Admin" WHERE "userId" = app_user_id)
INTO has_admin_profile;
SELECT EXISTS (SELECT 1 FROM public."Participant" WHERE "userId" = app_user_id)
INTO has_participant_profile;
IF has_admin_profile = has_participant_profile THEN
RETURN jsonb_build_object(
'error',
jsonb_build_object(
'http_code', 403,
'message', 'User must have exactly one application profile.'
)
);
END IF;
RETURN event;
END;
$$;
GRANT EXECUTE
ON FUNCTION public.custom_access_token_hook(jsonb)
TO supabase_auth_admin;
REVOKE EXECUTE
ON FUNCTION public.custom_access_token_hook(jsonb)
FROM authenticated, anon, public;
-- The hook runs as supabase_auth_admin, so it needs read access to the profile tables used above
GRANT SELECT
ON TABLE public."User", public."Admin", public."Participant"
TO supabase_auth_admin;
GRANT USAGE ON SCHEMA "public" TO supabase_auth_admin;
COMMIT;