Overview
GitaChat uses Clerk for authentication on user-specific endpoints. Public endpoints like semantic search and verse retrieval can be accessed without authentication, while features like bookmarks, notes, and history require a valid Clerk session.Public Endpoints
No authentication required
- Query search
- Get verse
- Get all verses
- Health check
Protected Endpoints
Clerk authentication required
- Bookmarks
- Notes
- History
- Daily verse
- Email subscriptions
- Image generation
Authentication Methods
GitaChat supports multiple authentication approaches depending on your use case.Frontend Web Application
For Next.js or React applications, use Clerk’s official SDKs:Backend API Requests
For server-to-server or mobile app requests, use Clerk session tokens. Getting a Session Token (Client):API Keys
GitaChat does not currently support traditional API keys. All authentication is handled through Clerk sessions for user-specific operations. For public endpoints (query, verse, all-verses), no authentication is needed:Setting Up Clerk
To integrate Clerk authentication in your application:1. Create a Clerk Account
Sign up at clerk.com and create a new application.2. Get Your API Keys
Publishable Key
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEYUsed in client-side code
Secret Key
CLERK_SECRET_KEYUsed in server-side code only
3. Configure Environment Variables
Create a.env.local file:
4. Install Clerk Middleware (Next.js)
Createmiddleware.ts in your project root:
Authorization Checks
GitaChat implements server-side authorization checks to ensure users can only access their own data.User ID Verification
All protected endpoints verify that the authenticated user ID matches the requested resource:Row Level Security (RLS)
GitaChat uses Supabase with Row Level Security enabled:CORS Configuration
The GitaChat backend is configured to accept requests from specific origins: Allowed Origins:http://localhost:3000(development)https://gitachat.org(production)https://www.gitachat.org(production)
- ✅ Cookies and credentials are sent with requests
- ✅ Frontend can make authenticated requests
- ✅ CORS preflight requests are handled
- ❌ Requests from other domains will be blocked
Error Responses
Authentication errors return standard HTTP status codes:401 Unauthorized
No valid session token provided:403 Forbidden
Valid session but insufficient permissions:503 Service Unavailable
Database not configured:SUPABASE_URL and SUPABASE_ANON_KEY environment variables
Rate Limiting with Authentication
Rate limits apply differently for authenticated vs. unauthenticated users:Unauthenticated
Based on IP AddressShared limit across all requests from same IP
Authenticated
Based on User IDIndividual limit per authenticated user
Authentication Best Practices
Secure Token Storage
Never expose tokens in:
- Git repositories
- Client-side code (for secret keys)
- Browser localStorage (use httpOnly cookies)
- Error messages or logs
Token Refresh
Implement automatic token refresh:
- Clerk handles this automatically
- Tokens expire after 1 hour by default
- Silent refresh keeps users signed in
Server-Side Validation
Always validate on the server:
- Never trust client-side authentication
- Use
auth()in API routes - Verify user ID matches resource owner
HTTPS Only
Always use HTTPS in production:
- Protects tokens in transit
- Prevents man-in-the-middle attacks
- Required for secure cookies
Example: Complete Protected Endpoint
Here’s a complete example of a protected API endpoint with all best practices:Testing Authentication
Local Development
For local testing, create a test user in your Clerk dashboard:- Go to Clerk Dashboard → Users
- Create a test user with email
- Use the Clerk dev environment for testing
- Sign in through your local app at
http://localhost:3000
Production Testing
For production, use Clerk’s production keys:Troubleshooting
401 Errors
Problem: Requests return 401 UnauthorizedSolutions:
- Verify Clerk is properly configured
- Check that session token is being sent
- Ensure token hasn’t expired
- Verify middleware is configured
CORS Errors
Problem: Browser blocks requestsSolutions:
- Verify origin is in allowed list
- Check credentials are enabled
- Ensure proper headers are sent
- Use correct protocol (http/https)
Token Issues
Problem: Token validation failsSolutions:
- Check Clerk secret key is correct
- Verify token format (Bearer prefix)
- Ensure token hasn’t expired
- Check clock sync on servers
503 Errors
Problem: Database not configuredSolutions:
- Set SUPABASE_URL environment variable
- Set SUPABASE_ANON_KEY environment variable
- Verify Supabase project is active
- Check network connectivity
Security Considerations
Never Expose Secret Keys
Validate All Inputs
Always Filter by User ID
Additional Resources
Clerk Documentation
Official Clerk documentation and guides
Next.js Integration
Clerk + Next.js App Router setup
Supabase RLS
Row Level Security policies
GitaChat API
Full API reference documentation
Need Help?
For authentication issues or questions:- Check the Clerk community forums
- Review GitaChat API docs
- Verify your environment variables are set correctly
- Test with Clerk’s development environment first