Skip to content

Commit 0c9110b

Browse files
committed
docs: update jwt claims doc
1 parent 8c7b5d7 commit 0c9110b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/postgresql/RLS_AND_JWT_CLAIMS.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,39 @@ current_setting('request.jwt.claims')
125125

126126
---
127127

128+
## Optional: Helper Functions for JWT Claims
129+
130+
CloudSync validates JWTs and passes all claims to PostgreSQL via `request.jwt.claims` — no PostgreSQL extension is required for JWT verification. The validation happens entirely in the CloudSync microservice.
131+
132+
However, writing `(current_setting('request.jwt.claims')::jsonb->>'sub')::uuid` in every RLS policy is verbose. Following the pattern used by Supabase and Neon, you can optionally create a small set of helper functions in a dedicated schema:
133+
134+
```sql
135+
-- Create a schema for auth helpers (optional, but keeps things clean)
136+
CREATE SCHEMA IF NOT EXISTS auth;
137+
138+
-- Returns all JWT claims as JSONB
139+
CREATE OR REPLACE FUNCTION auth.session()
140+
RETURNS jsonb AS $$
141+
SELECT current_setting('request.jwt.claims', true)::jsonb;
142+
$$ LANGUAGE SQL STABLE;
143+
144+
-- Returns the user ID (sub claim)
145+
CREATE OR REPLACE FUNCTION auth.user_id()
146+
RETURNS text AS $$
147+
SELECT auth.session()->>'sub';
148+
$$ LANGUAGE SQL STABLE;
149+
150+
-- Returns the user's role claim
151+
CREATE OR REPLACE FUNCTION auth.role()
152+
RETURNS text AS $$
153+
SELECT auth.session()->>'role';
154+
$$ LANGUAGE SQL STABLE;
155+
```
156+
157+
> **Note:** These are just convenience wrappers — they read from the same `request.jwt.claims` session variable that CloudSync sets.
158+
159+
---
160+
128161
## Security Rules
129162

130163
### Rule 1: Use Immutable Claims for RLS

0 commit comments

Comments
 (0)