Skip to content

Conversation

@zoedsoupe
Copy link
Member

Problem

When using Ecto schemas with Supabase databases that have Row Level Security (RLS) policies enabled, there's no built-in way
to set PostgreSQL session variables (like request.jwt.claims.sub) that RLS policies depend on for filtering data based on
the authenticated user context.

Solution

Added Supabase.PostgREST.RLS module with helpers for setting RLS context in Ecto transactions:

  • set_rls_context/3 - Sets PostgreSQL session config variables transaction-scoped
  • set_user_id/2, set_user_role/2 - Convenience wrappers for common Supabase JWT claims
  • set_rls_claims/2 - Batch set multiple claims at once
  • with_user_context/3 - Wraps operations in a transaction with automatic user context

Rationale

Transaction-scoped session variables ensure RLS context is automatically cleaned up and doesn't leak between requests. The API
follows Supabase's standard JWT claims pattern (request.jwt.claims.*) and integrates naturally with Ecto's transaction
system. Made ecto_sql an optional dependency to keep the library lightweight for users not using Ecto.

@zoedsoupe zoedsoupe requested a review from Ziinc October 28, 2025 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants