Overview
The SkillRise API uses Clerk for authentication and authorization. Clerk provides secure session management with JWT tokens that include user identity and role-based metadata.Authentication Flow
1
User Signs In
User authenticates through Clerk on the frontend (sign-up/sign-in)
2
Clerk Issues Token
Clerk generates a JWT session token containing:
userId- Unique user identifiersessionClaims.metadata.role- User role (student, educator, admin)
3
Frontend Includes Token
Frontend includes the Clerk session token in API requests (handled automatically by Clerk SDK)
4
API Validates Token
Clerk middleware validates the token and populates
req.auth with user context5
Role Check (if required)
Protected routes verify user role via custom middleware
Clerk Middleware
The API uses@clerk/express middleware globally:
- Validates Clerk session tokens
- Populates
req.authwith authentication context - Allows both authenticated and unauthenticated requests
Auth Context (req.auth)
On authenticated requests, req.auth contains:
string
required
Clerk user ID (used as MongoDB
_id for User documents)object
Clerk session claims object
string
User role:
"student", "educator", or "admin"Protected Routes
User Authentication
User-specific endpoints require authentication via Clerk’srequireAuth() middleware:
/api/user/* are protected:
/api/user/data- Get user profile/api/user/enrolled-courses- Get enrolled courses/api/user/purchase- Purchase course/api/user/ai-chat- AI chatbot- And more…
401 Unauthorized
Role-Based Access Control
SkillRise implements three user roles with distinct permissions:Student (Default)
All authenticated users have student access by default. Students can:- Browse and purchase courses
- Track progress and earn certificates
- Use AI chat assistant
- Participate in community discussions
- Take quizzes
Educator
Educators can create and manage courses. Educator routes use custom middleware:POST /api/educator/add-course- Create new courseGET /api/educator/courses- List educator’s coursesGET /api/educator/dashboard- Dashboard analyticsGET /api/educator/enrolled-students- Student enrollment dataPOST /api/quiz/generate- Generate AI quizGET /api/quiz/educator-insights- Quiz performance insights
403 Forbidden
Admin
Admins have full platform access. Admin routes use custom middleware:GET /api/admin/stats- Platform statisticsGET /api/admin/chart-data- Analytics chartsGET /api/admin/courses- All coursesGET /api/admin/purchases- All purchasesGET /api/admin/users- User managementGET /api/admin/educator-applications- Educator applicationsPATCH /api/admin/educator-applications/:id/approve- Approve educatorPATCH /api/admin/educator-applications/:id/reject- Reject educator
403 Forbidden
Becoming an Educator
Users can apply to become educators through the application system:1
Submit Application
POST /api/educator/apply - Submit educator application (no role required)2
Admin Review
Admin reviews application via
/api/admin/educator-applications3
Approval/Rejection
Admin approves or rejects via PATCH endpoints
4
Role Updated
On approval, user’s Clerk metadata is updated with
role: "educator"Public Endpoints
Some endpoints are publicly accessible without authentication:Public
List all published courses
Public
Get course details (lecture URLs hidden for non-free content)
Public
Verify certificate authenticity via QR code
Public
List community groups (membership requires auth)
Public
Browse community posts (voting/posting requires auth)
Webhooks Authentication
Clerk Webhooks
Clerk webhooks use Svix signature verification:Razorpay Webhooks
Razorpay webhooks use HMAC signature verification:Example: Making Authenticated Requests
Frontend (with Clerk React)
@clerk/clerk-react hooks.
Direct API Call (with token)
Security Best Practices
Rate limiting is tied to
userId when authenticated, preventing users from bypassing limits by switching IP addresses.Token Validation
- Tokens are validated on every request via Clerk middleware
- Expired tokens receive
401 Unauthorized - Role changes take effect immediately (no token refresh needed)
CORS Protection
API accepts requests only from configured frontend origin:Troubleshooting
401 Unauthorized
Expired Session
Expired Session
User needs to re-authenticate. Clerk SDK handles this automatically.
Invalid Token
Invalid Token
Token may be malformed or from wrong Clerk instance. Verify
CLERK_PUBLISHABLE_KEY matches backend.403 Forbidden
Insufficient Role
Insufficient Role
User lacks required role (educator/admin) for this endpoint. Check
req.auth.sessionClaims.metadata.role.Course Not Purchased
Course Not Purchased
User attempting to access enrolled course content without purchasing. Verify enrollment via
/api/user/enrolled-courses.Next Steps
API Overview
Learn about API structure and response formats
Endpoints
Browse all available API endpoints