Auth & RBAC
Technical design of Clerk integration and Role-Based Access Control
Overview
Danvas uses Clerk for authentication and identity management. Multi-tenancy is implemented using Clerk Organizations, where each organization represents a Team in our database.
Clerk Integration
User Provisioning
When a user signs in for the first time, our middleware calls provisionUser() in @repo/auth.
- Webhook Sync: Clerk webhooks (
user.created,user.updated) sync user data to ouruserstable. - Metadata: Role and Location assignments are stored in Clerk
publicMetadatato enable efficient client-side checks and guard against unauthorized access at the edge. - Setup Flow:
/onboardingcollects optional profile details like phone and job roles, then marksusers.onboardingCompletein the local database. Profile setup is separate from team assignment.
Middleware
The @repo/auth package provides middleware that handles:
- Session validation.
- Redirecting unauthenticated users to sign-in.
- Ensuring users belong to an active team.
- Resolving the
teamIdfrom the active organization.
Role-Based Access Control (RBAC)
We support three primary roles, stored in the users table and Clerk metadata.
| Role | Permissions |
|---|---|
admin | Full access to all team locations, billing, and settings. |
manager | Access to specific locations, reports, and scheduling. |
member | End-user access (staff) to filing reports and viewing schedules. |
Auth Guards
Use these helpers in Server Components and Actions to enforce security: