Overview
SkillRise uses Clerk for authentication and user management. Clerk handles user sign-up, sign-in, profile management, and session management. User data is synced to your MongoDB database via webhooks.Features
- Authentication: Email/password, OAuth (Google, GitHub, etc.)
- Role-based access control: Student, Educator, Admin roles
- Webhook sync: Automatic user sync to MongoDB
- Session management: Secure JWT-based sessions
- Profile management: User profile updates synced automatically
Environment Variables
Server Configuration
Add these to yourserver/.env file:
server/.env
Client Configuration
Add this to yourclient/.env file:
client/.env
Get your keys from the Clerk Dashboard. Create a new application if you haven’t already.
Setup Instructions
1
Create Clerk Application
- Go to Clerk Dashboard
- Click Add application
- Name your application (e.g., “SkillRise”)
- Choose authentication methods (Email, Google, GitHub, etc.)
- Click Create application
2
Configure API Keys
- From the Clerk Dashboard, navigate to API Keys
- Copy the Publishable Key and Secret Key
- Add them to your
.envfiles (server and client)
3
Set Up Custom Metadata
Clerk stores user roles in session claims metadata. Configure this in your Clerk Dashboard:This allows the server to access
- Go to Sessions → Customize session token
- Add this JSON to include role metadata:
req.auth.sessionClaims.metadata.role.4
Configure Webhooks
Clerk webhooks sync user data to your MongoDB database.
- Go to Webhooks in Clerk Dashboard
- Click Add Endpoint
- Enter your webhook URL:
- Development: Use ngrok →
https://your-ngrok-url.ngrok.io/clerk - Production:
https://your-domain.com/clerk
- Development: Use ngrok →
- Subscribe to these events:
user.createduser.updateduser.deleted
- Copy the Signing Secret and add it to
server/.envasCLERK_WEBHOOK_SECRET
Role-Based Access Control
SkillRise implements three user roles:Setting User Roles
Roles are stored in Clerk’spublicMetadata. You can set roles:
Via Clerk Dashboard:
- Go to Users
- Select a user
- Scroll to Metadata → Public metadata
- Add:
Protecting Routes
SkillRise uses middleware to protect routes by role:server/middlewares/authMiddleware.js
server/routes/educatorRoutes.js
Webhook Implementation
The Clerk webhook endpoint syncs user data to MongoDB:server/controllers/webhooks.js
server/server.js
Server Middleware Setup
Initialize Clerk middleware in your Express app:server/server.js
- Validates session tokens
- Attaches
req.authwith user info - Makes
req.auth.userIdandreq.auth.sessionClaimsavailable
Client Integration
Wrap Your App
client/src/main.jsx
Access User Data
Protect Routes
Testing Webhooks Locally
1
Install ngrok
2
Start your server
3
Expose localhost with ngrok
https://abc123.ngrok.io)4
Update Clerk webhook URL
In Clerk Dashboard → Webhooks → Edit your endpoint:
5
Test webhook
Create a new user in Clerk Dashboard or sign up via your app. Check your server logs and MongoDB to verify the user was created.
Common Issues
Webhook signature verification fails
Webhook signature verification fails
- Ensure
CLERK_WEBHOOK_SECRETmatches the signing secret in Clerk Dashboard - Verify you’re using the correct webhook endpoint URL
- Check that headers
svix-id,svix-timestamp, andsvix-signatureare being sent
Role not accessible in routes
Role not accessible in routes
- Verify you’ve customized the session token in Clerk Dashboard (Sessions → Customize session token)
- Ensure the role is set in
publicMetadata, notprivateMetadata - Check that
clerkMiddleware()is applied before your routes
User not syncing to MongoDB
User not syncing to MongoDB
- Check your webhook is subscribed to
user.created,user.updated,user.deletedevents - Verify your MongoDB connection is active
- Check server logs for webhook errors
Resources
Clerk Documentation
Official Clerk documentation
Express SDK
Clerk Express SDK reference
React SDK
Clerk React SDK reference
Webhooks
Webhook integration guide