CRITICAL story-supabase-backend-core-global-admin-004 5 pts
5
Story Points
Critical
Priority
Supabase Backend Core
Feature

User Story

As a Global Administrator
I want to configure the JWT token structure to include custom claims for user role and organization membership
So that So that RLS policies can enforce organization-scoped data access without requiring additional database lookups on every request

Acceptance Criteria

  • Given a user authenticates successfully, when the JWT is decoded, then it contains role, organization_id, and local_association_id claims matching the user's database record
  • Given a user belongs to multiple organizations, when their JWT is generated, then it includes claims for all applicable organization memberships
  • Given a user's role changes in the database, when they refresh their token, then the new JWT reflects the updated role claim within the configured token refresh window
  • Given an RLS policy references auth.jwt()->>'organization_id', when a query executes, then the claim is correctly evaluated against the row's organization column
  • Given the admin updates the JWT template, when existing sessions make requests, then the old tokens continue working until expiry and new tokens use the updated claim structure

Business Value

Custom JWT claims are the bridge between Supabase Auth and RLS policies. Without them, every database request would require a secondary lookup to determine the user's role and organization — doubling query latency and introducing race conditions. Embedding these in the JWT ensures atomic, consistent access control that scales across all 60+ tables without performance degradation.