Clerk Authentication Setup
ZapDev uses Clerk for user authentication, providing secure login, session management, and JWT token generation for Convex database access.Create a Clerk Account
- Go to Clerk Dashboard and sign up
- Create a new application (e.g., “ZapDev”)
- Choose your authentication methods (Email, Google, GitHub, etc.)
Get Your API Keys
After creating your application, navigate to API Keys in the Clerk dashboard. You’ll need:- Publishable Key - Used in the frontend
- Secret Key - Used in the backend
Add Keys to Environment
Update your.env file:
Configure JWT Template for Convex
Clerk needs to generate JWT tokens that Convex can verify. This requires creating a custom JWT template.Create Convex JWT Template
- In Clerk Dashboard, go to JWT Templates
- Click New Template → Convex
- Name it exactly:
convex - Click Create
Get JWT Issuer Domain
After creating the template:- Click on the
convextemplate - Copy the Issuer Domain (e.g.,
https://your-app.clerk.accounts.dev) - Add it to
.env:
Configure Convex to Accept Clerk Tokens
In your Convex dashboard:- Go to Settings → Auth
- Add Clerk as an auth provider
- Enter your JWT Issuer Domain from Clerk
- Save the configuration
Convex will now validate JWT tokens issued by Clerk, enabling secure database access based on user identity.
Configure Sign-In/Sign-Up Routes
ZapDev uses custom routes for authentication flows. Add these to your.env:
How It Works
- Sign In URL: Users are redirected to
/sign-inwhen authentication is required - Sign Up URL: New users are directed to
/sign-upfor account creation - Fallback Redirects: After authentication, users are sent to
/(home page)
Server-Side Authentication
ZapDev includes server-side authentication helpers insrc/lib/auth-server.ts for use in Server Components and API routes.
Get Current User
Get JWT Token
Get Authenticated Convex Client
- Creates a Convex HTTP client
- Retrieves the user’s JWT token
- Sets the token for authenticated requests
All Convex mutations and queries from the server should use
getConvexClientWithAuth() to ensure proper user context.Client-Side Authentication
Clerk provides React hooks for client-side authentication:Check Authentication Status
Sign Out Button
Protecting Routes
Server Components
UsegetUser() to protect server-rendered pages:
Middleware Protection
For route-level protection, use Clerk’s middleware inmiddleware.ts:
Convex Integration
All Convex queries and mutations can access the authenticated user viactx.auth:
User Identity Fields
identity.subject- Clerk user ID (use this asuserIdin your database)identity.email- User’s email addressidentity.name- User’s full nameidentity.pictureUrl- Profile picture URL
Testing Authentication
Create a Test User
- Start your dev server:
npm run dev - Navigate to http://localhost:3000/sign-up
- Create an account using email or social login
- Verify the user appears in the Clerk Dashboard → Users
Test Protected Routes
- Visit a protected page while signed out (should redirect to
/sign-in) - Sign in and verify access is granted
- Sign out and confirm redirect behavior
Verify JWT Token
In a Server Action or API route:Production Configuration
Update Production Keys
When deploying to production:- Create a Production instance in Clerk (separate from Development)
- Copy the production API keys
- Update environment variables in Vercel:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYCLERK_SECRET_KEYCLERK_JWT_ISSUER_DOMAIN
Configure Production Domain
- In Clerk Dashboard → Domains, add your production domain
- Update redirect URLs to use
https://your-domain.com - Configure CORS settings if using custom domains
Enable Production Features
- Email Verification: Require email verification for new signups
- 2FA: Enable two-factor authentication for enhanced security
- Session Limits: Configure session duration and token expiration
- Webhooks: Set up Clerk webhooks to sync user data with your database
Troubleshooting
”Invalid JWT” Errors
- Verify
CLERK_JWT_ISSUER_DOMAINmatches the issuer in your JWT template - Check that the JWT template name is exactly
convex - Ensure Convex is configured with the correct Clerk issuer domain
Sign-In Redirect Loop
- Check that
NEXT_PUBLIC_CLERK_SIGN_IN_URLis set to/sign-in - Verify middleware is not blocking auth routes
- Clear browser cookies and try again
User Identity Null in Convex
- Confirm the user is authenticated on the client
- Verify the Convex client is using
useConvexAuth()orgetConvexClientWithAuth() - Check that the JWT token is being sent with requests (inspect network tab)
Profile Picture Not Loading
- Ensure
imageUrlis not null in user object - Check CORS settings allow loading images from Clerk’s CDN
- Verify the image URL is valid (test in browser)
Next Steps
- Configure E2B Sandboxes
- Set up Inngest
- Deploy to Vercel with production environment variables
- Configure Clerk webhooks for user sync