Overview
Quest Hunter uses Clerk for authentication and user management. The Convex backend integrates with Clerk through webhooks to synchronize user data.Authentication Configuration
The authentication is configured inconvex/auth.config.ts:
Environment Variables
The JWT issuer domain from your Clerk application settings
The webhook secret for verifying Clerk webhook requests
Authentication Enforcement
All Quest Hunter APIs require authentication. TherequireUser utility function enforces this:
Error Handling
When authentication fails, you’ll receive one of these errors:- “Not authenticated” - No valid JWT token provided
- “User not found” - JWT is valid but user doesn’t exist in the database (shouldn’t happen if webhooks are working correctly)
Clerk Webhook Integration
Quest Hunter uses Clerk webhooks to keep user data synchronized. The webhook endpoint is available at/clerk-webhook.
Webhook Events
The webhook handles three event types:user.created
Triggered when a new user signs up. Creates a new user record in Convex.user.updated
Triggered when a user updates their profile. Updates the existing user record with new data.user.deleted
Triggered when a user is deleted from Clerk. Removes the user from Convex.Webhook Request Format
Clerk sends webhooks with these headers:Unique message identifier
Unix timestamp of when the message was sent
Signature for verifying the webhook authenticity
Webhook Payload
Webhook Security
The webhook verifies all requests using thesvix library:
- 500 “Missing CLERK_WEBHOOK_SECRET” - Environment variable not configured
- 400 “Missing svix headers” - Required Svix headers not present
- 400 “Invalid webhook signature” - Signature verification failed
User Schema
Users are stored with the following structure:Convex-generated unique identifier
The unique Clerk user ID (indexed)
User’s primary email address
User’s first name (optional)
User’s last name (optional)
URL to the user’s profile image (optional)
Internal Mutations
These mutations are called internally by the webhook handler and are not directly accessible from client code.upsertUser
Creates or updates a user based on Clerk webhook data.The Clerk user ID
User’s email address
User’s first name
User’s last name
URL to user’s profile image
deleteUser
Deletes a user when they’re removed from Clerk.The Clerk user ID to delete
Setup Instructions
- Configure Clerk provider in your Convex dashboard with your Clerk JWT issuer domain
-
Set environment variables in your Convex deployment:
-
Configure Clerk webhook to point to your Convex deployment:
-
Enable webhook events in Clerk dashboard:
- user.created
- user.updated
- user.deleted