Authentication flow
Clerk handles authentication automatically through middleware. When you make requests to protected API endpoints, Clerk validates your session and provides user information to the API.How it works
- User signs in through the GitRead web interface
- Clerk creates a session and issues session tokens
- Session tokens are automatically included in API requests via cookies
- The API validates the session and extracts the
userId - The
userIdis used to access user-specific resources
You don’t need to manually include authentication headers when using the GitRead web application - Clerk handles this automatically.
Protected endpoints
The following endpoints require authentication:POST /api/generate- Generate README filesGET /api/credits- View credit balancePOST /api/credits- Update creditsGET /api/readme-history- Retrieve generation historyPOST /api/readme-history- Save README to historyPOST /api/create-checkout-session- Create payment sessionPOST /api/verify-payment- Verify payments
Public endpoints
These routes are accessible without authentication:/- Home page/terms- Terms of service/privacy- Privacy policy/support- Support page/api/health- Health check endpoint
Authentication in API routes
Each API route validates authentication using Clerk’sgetAuth helper:
Handling unauthorized requests
When you make a request without valid authentication, the API returns a 401 Unauthorized response:The exact error message may vary by endpoint but will always include a 401 status code.
User identification
TheuserId extracted from Clerk sessions is used to:
- Track user credit balance in the database
- Associate generated READMEs with users
- Verify payment ownership
- Enforce per-user limits and quotas
Session management
Clerk sessions are managed through middleware configuration inmiddleware.ts:
Environment variables
Clerk requires the following environment variables:These variables configure Clerk’s behavior and define the authentication flow paths.
Best practices
Always check authentication status
Always check authentication status
Every protected endpoint should validate the
userId before processing requests. Return a 401 error if authentication fails.Use service role for admin operations
Use service role for admin operations
When accessing Supabase from API routes, use the service role key for operations that require elevated permissions:
Don't expose sensitive data
Don't expose sensitive data
Never return sensitive authentication tokens or keys in API responses. Keep credentials server-side only.
Validate user ownership
Validate user ownership
When accessing user-specific resources, always verify that the authenticated user owns the resource:
Troubleshooting
”Unauthorized” errors
If you’re receiving 401 errors:- Ensure you’re signed in to GitRead
- Check that your session hasn’t expired
- Verify Clerk environment variables are configured
- Clear cookies and sign in again
Session not persisting
If sessions aren’t persisting across requests:- Check that cookies are enabled in your browser
- Verify the
NEXT_PUBLIC_APP_URLmatches your domain - Ensure middleware is properly configured
For additional help with Clerk authentication, refer to the Clerk documentation.