Authentication
ZapDev uses Clerk for authentication with JWT (JSON Web Token) based authorization. All authenticated API requests require a valid Clerk JWT token.Authentication Flow
- User signs in through Clerk (OAuth, email/password, etc.)
- Clerk generates a JWT token with user identity
- Token is included in API requests to authenticate the user
- Server validates the token and extracts user identity
- Protected endpoints verify user authorization
Clerk JWT Configuration
Convex Authentication
Convex is configured to accept Clerk JWTs throughconvex/auth.config.ts:
CLERK_JWT_ISSUER_DOMAIN: Your Clerk instance domain (e.g.,clerk.your-app.com)CLERK_JWT_TEMPLATE_NAME: JWT template name (default:"convex")
Server-Side Token Retrieval
Thesrc/lib/auth-server.ts module provides utilities for retrieving and using Clerk tokens:
Authentication Helpers
Convex Authentication Helpers
Convex provides several authentication helpers inconvex/helpers.ts:
getCurrentUserId(ctx)
Returns the current user’s Clerk ID or null if not authenticated.
requireAuth(ctx)
Requires authentication and throws an error if the user is not authenticated. Returns the user’s Clerk ID.
convex/projects.ts:287):
User Information
Getting User Details
ThegetUser() function from src/lib/auth-server.ts:14 retrieves the current user’s information:
Authenticated Convex Client
For server-side operations, use the authenticated Convex client (src/lib/auth-server.ts:48):
Authorization Patterns
Resource Ownership Verification
Always verify that the authenticated user owns the resource they’re accessing:Subscription-Based Access
Check subscription tiers using helper functions inconvex/helpers.ts:20:
hasUnlimitedAccess(ctx): Check for unlimited tierhasPlan(ctx, planProductId): Check for specific planhasFeature(ctx, featureId): Check for specific feature access
Security Best Practices
- Always Use Authentication Helpers: Use
requireAuth()in all protected Convex functions - Verify Resource Ownership: Check that
project.userId === userIdbefore mutations - Never Expose Internal IDs: Don’t return Clerk user IDs in public responses
- Use Indexes for Queries: Filter by
userIdusing indexes, not.filter() - Handle Token Expiry: Implement proper error handling for expired tokens
- Validate All Inputs: Use Convex validators (
v.*) for all function arguments
Error Handling
Unauthorized Errors
Convex:Client-Side Handling
Next Steps
- API Overview - Learn about the Convex API architecture
- Projects API - Project management with authentication
- Usage API - Credit tracking and subscription tiers