Authentication Overview
The Los Inmaduros Backend API uses Clerk for authentication and authorization. Authenticated endpoints require a valid JWT (JSON Web Token) issued by Clerk to be included in the request headers.Authentication Method
The API uses Bearer Token authentication. When making authenticated requests, you must include anAuthorization header with your JWT token:
How It Works
- User Authentication: Users authenticate through Clerk on the frontend application
- Token Generation: Clerk generates a JWT token for the authenticated session
- Token Verification: The backend verifies the token using Clerk’s SDK
- User Synchronization: The backend automatically creates or retrieves the user record in the database
- Request Authorization: The authenticated user’s ID and role are attached to the request
Authorization Header Format
Include the JWT token in theAuthorization header with the Bearer prefix:
Authenticated Request Example
Here’s a complete example of making an authenticated request usingcurl:
Creating a Review (POST Example)
Authentication Levels
The API implements three levels of authentication:1. Public Endpoints (No Auth Required)
These endpoints are accessible without authentication:GET /api/routes- Get all routesGET /api/routes/:slug- Get route detailsGET /api/route-calls- Get all route callsGET /api/route-calls/:id- Get route call by IDGET /api/photos- Get public photosGET /api/routes/:slug/gallery- Get route galleryGET /api/route-calls/:id/gallery- Get route call gallery
2. Authenticated Endpoints (Auth Required)
These endpoints require a valid JWT token: Reviews:POST /api/routes/:routeId/reviews- Create reviewGET /api/reviews/my-reviews- Get user’s reviewsPUT /api/reviews/:reviewId- Update review (owner only)DELETE /api/reviews/:reviewId- Delete review (owner/admin only)
GET /api/favorites- Get user’s favoritesGET /api/favorites/check/:routeId- Check if route is favoritedPOST /api/routes/:routeId/favorites- Add to favoritesDELETE /api/routes/:routeId/favorites/:favoriteId- Remove from favorites
POST /api/route-calls- Create route callPUT /api/route-calls/:id- Update route call (organizer only)PATCH /api/route-calls/:id/cancel- Cancel route call (organizer/admin only)DELETE /api/route-calls/:id- Delete route call (organizer/admin only)
GET /api/attendances/my-attendances- Get user’s attendancesPOST /api/route-calls/:routeCallId/attendances- Confirm attendanceDELETE /api/route-calls/:routeCallId/attendances/:attendanceId- Cancel attendance
POST /api/photos- Upload photoGET /api/photos/my-photos- Get user’s photosDELETE /api/photos/:id- Delete photo (owner/admin only)PATCH /api/route-calls/:id/cover-photo- Update cover photo (organizer only)
3. Admin-Only Endpoints (Admin Role Required)
These endpoints require authentication AND the user must have theADMIN role:
GET /api/photos/pending-review- Get photos pending moderationPATCH /api/photos/:id/approve- Approve photoPATCH /api/photos/:id/reject- Reject photo
Authentication Middleware
The backend uses three authentication middleware functions:requireAuth
Requires a valid authentication token. Adds auth object to the request:
requireAdmin
Must be used after requireAuth. Restricts access to users with ADMIN role.
optionalAuth
Adds auth to request if token is present, but doesn’t require it. Useful for endpoints that provide different data based on authentication status.
Error Responses
401 Unauthorized
Returned when authentication is required but no valid token is provided:403 Forbidden
Returned when authenticated but lacking required permissions:Development Test Token (Development Only)
For testing purposes in development, you can generate a test JWT token:The user must already exist in Clerk before generating a test token. Create users through the Clerk dashboard first.
Security Best Practices
- Never share tokens: Keep your JWT tokens secure and never commit them to version control
- Token expiration: Tokens expire automatically - implement token refresh in your frontend
- HTTPS in production: Always use HTTPS in production to encrypt token transmission
- Secure storage: Store tokens securely in your frontend (httpOnly cookies or secure storage)
- Remove test endpoint: Ensure the
/api/auth/test-tokenendpoint is disabled in production
Integration Example
Here’s a complete example of integrating authentication in a JavaScript/TypeScript frontend:Clerk Configuration
The backend requires the following Clerk environment variable to be configured:.env file for the authentication to work correctly.