Why Clerk?
- Organizations Built-in - Multi-tenant architecture with role-based access
- Zero Backend Auth Code - Fully managed authentication
- Beautiful UI Components - Pre-built sign-in/sign-up flows
- Social Sign-In - Google, GitHub, Microsoft, and more
- Free Tier - 10,000 monthly active users
Prerequisites
- Clerk account (sign up free)
- Application created in Clerk Dashboard
Quick Setup
Create Clerk application
- Go to Clerk Dashboard
- Click Create Application
- Name your app:
Resonance(or your preferred name) - Choose authentication methods:
- Email + Password
- Social sign-in (Google, GitHub, etc.)
- Click Create Application
Enable Organizations
Organizations are required for multi-tenancy:
- Navigate to Configure → Organizations
- Enable Organizations
- Configure settings:
- Creation: Allow users to create organizations
- Membership: Invite-based or open
- Roles: Admin, Member (default)
- Click Save
Each Clerk organization maps to a separate tenant in Resonance with isolated voices and generations.
Copy API keys
Navigate to API Keys and copy:
- Publishable Key (starts with
pk_) - Secret Key (starts with
sk_)
Integration Overview
Clerk is integrated throughout the application:Middleware
Protects all routes except public pages:src/middleware.ts
Auth Pages
Pre-built sign-in and sign-up components:Organization Context
Access the current organization ID in tRPC procedures:src/trpc/init.ts
Multi-Tenancy
Resonance uses Clerk Organizations for multi-tenant data isolation.Data Scoping
All custom voices and generations are scoped byorgId:
System Voices
System voices (variant: SYSTEM) have orgId: null and are visible to all organizations:
Organization Roles
Clerk supports role-based access control within organizations:| Role | Permissions |
|---|---|
| Admin | Full access - manage members, voices, billing |
| Member | Read/write voices, create generations |
Resonance uses the default Clerk roles. You can customize roles in Clerk Dashboard → Organizations → Roles.
User Management
Organization Switcher
Clerk provides a built-in component for switching between organizations:- Switch active organization
- Create new organizations
- Manage members and invitations
User Button
Account management dropdown:Billing Integration
Clerk organization IDs are used as external customer IDs in Polar:src/trpc/routers/billing.ts
src/trpc/routers/billing.ts:7.
Benefits:
- No separate customer database
- Automatic customer creation on first checkout
- Organization-level billing (not per-user)
Authentication Methods
Email + Password
Default authentication method. Users can sign up with email and password.Social Sign-In
Enable OAuth providers in Clerk Dashboard → User & Authentication → Social Connections:- GitHub
- Microsoft
- Apple
- And 20+ more
Magic Links
Passwordless authentication via email:- Enable in User & Authentication → Email, Phone, Username
- Toggle Email verification links
- Configure email template (optional)
Development vs Production
Development Keys
Test keys (start withpk_test_ and sk_test_):
- Free unlimited usage
- Separate user database from production
- Use for local development
Production Keys
Live keys (start withpk_live_ and sk_live_):
- Subject to pricing plan limits
- Real user data
- Require domain verification
Domain Configuration
For custom domains in production:Session Management
Clerk handles session management automatically:- Sessions are stored in secure HTTP-only cookies
- Automatic token refresh
- Session expires after 7 days of inactivity (configurable)
Server-Side Auth
Client-Side Auth
Troubleshooting
Middleware infinite redirect
If you get stuck in a redirect loop:- Check that
/sign-inand/sign-upare not protected by middleware - Verify
matcherconfig insrc/middleware.tsexcludes auth pages - Clear cookies and try again
Organization not found
- Check Configure → Organizations is enabled
- Prompt users to create an organization on first sign-in
- Use
<OrganizationSwitcher />for easy access
Invalid Publishable Key
- Verify
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYhasNEXT_PUBLIC_prefix - Check key starts with
pk_test_orpk_live_ - Ensure no extra spaces or quotes
- Restart dev server after changing
.env.local
CORS errors in production
Add your production domain to Allowed Origins in Clerk Dashboard:- Go to Settings → Security
- Add
https://yourdomain.comto allowed origins - Redeploy application
Related Documentation
Environment Variables
Required Clerk environment variables
Polar Billing
Organization-based billing with Polar
Clerk Documentation
Official Clerk documentation