Clerk and Supabase integration patterns for user sync, JWT authentication, and RLS policies. Use when integrating Clerk authentication with Supabase, syncing user data between platforms, configuring RLS with Clerk JWT tokens, setting up webhooks for user events, implementing secure database access with Clerk identity, or when user mentions Clerk Supabase sync, user synchronization, JWT RLS, authentication webhooks, or database user management.
/plugin marketplace add vanman2024/ai-dev-marketplace/plugin install clerk@ai-dev-marketplaceThis skill is limited to using the following tools:
examples/complete-integration.tsxexamples/organization-access.tsxexamples/protected-route.tsxexamples/webhook-handler.tsscripts/configure-rls.shscripts/create-webhooks.shscripts/setup-sync.shscripts/sync-users.shscripts/test-jwt.shtemplates/clerk-jwt-template.jsontemplates/edge-function-webhook.tstemplates/env.exampletemplates/middleware-auth.tstemplates/rls-policies-clerk.sqltemplates/supabase-client-clerk.tstemplates/triggers.sqltemplates/user-schema.sqltemplates/webhook-sync.tsComprehensive integration patterns for syncing Clerk authentication with Supabase databases, including JWT configuration, RLS policies, webhook setup, and user data synchronization.
JWT Configuration Strategy
User Sync Architecture
RLS Policy Design
sub claim as user identifier in policiesWebhook Implementation
Phase 1: JWT Setup
Phase 2: Database Schema
Phase 3: Webhook Configuration
Phase 4: Client Integration
JWT Validation:
RLS Policies:
auth.uid() to extract Clerk user ID from JWTWebhook Security:
API Keys:
Webhook-Based Sync (Recommended):
Client-Side Sync:
Pattern 1: Public Profile with Private Data
users_public (readable by all, RLS enforced)
- clerk_id
- username
- avatar_url
users_private (readable only by owner)
- clerk_id
- email
- metadata
Pattern 2: Organization-Based Access
RLS Policy:
auth.jwt()->>'org_id' = organizations.clerk_org_id
Pattern 3: Role-Based Permissions
RLS Policy:
auth.jwt()->>'role' IN ('admin', 'editor')
JWT Validation Fails:
User Sync Delays:
RLS Policy Denies Access:
Automated setup and configuration scripts:
scripts/setup-sync.sh - Configure Supabase for Clerk JWT validationscripts/configure-rls.sh - Generate RLS policies for Clerk authenticationscripts/create-webhooks.sh - Deploy webhook infrastructurescripts/test-jwt.sh - Test JWT validation and claim extractionscripts/sync-users.sh - Manually trigger user synchronizationIntegration code templates for different scenarios:
templates/supabase-client-clerk.ts - Supabase client with Clerk session tokentemplates/webhook-sync.ts - Clerk webhook handler for user synctemplates/edge-function-webhook.ts - Supabase Edge Function webhooktemplates/middleware-auth.ts - Next.js middleware with Clerk + Supabasetemplates/rls-policies-clerk.sql - Comprehensive RLS policiestemplates/user-schema.sql - User tables schematemplates/triggers.sql - Database triggers for audit loggingtemplates/env.example - Environment variables templatetemplates/clerk-jwt-template.json - JWT template configurationComplete working examples:
examples/complete-integration.tsx - Full Next.js app with Clerk + Supabaseexamples/webhook-handler.ts - Production webhook implementationexamples/protected-route.tsx - Protected page with RLSexamples/organization-access.tsx - Multi-tenant with organizationsExport Users:
Update Database:
Deploy Webhooks:
Update Client:
Cutover:
Database Indexes:
CREATE INDEX idx_users_clerk_id ON users(clerk_id);
CREATE INDEX idx_orgs_clerk_org_id ON organizations(clerk_org_id);
Webhook Performance:
Client Performance:
Clerk:
Supabase:
Before going to production:
Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.