AI Resource Pro / website / ADMIN_AUTH.md
Admin Authentication System
Overview
This document explains the admin authentication system used in the AI Resource Pro project, along with recent security improvements.
Authentication Flow
The admin authentication system uses a combination of Supabase authentication and a custom admin check:
- Login Process:
- User enters email and password on
/admin/login - Credentials are validated against Supabase Auth
- User's admin status is verified in the database (
userstable withis_admin: true) - The
admin_session_activecookie is set only for validated admin users
- User enters email and password on
- Authentication Verification:
- Server-side: The
validateRequest()function insrc/lib/auth/supabase-server-auth.tschecks for the admin cookie and Supabase session - Middleware: Additional protection for admin routes in
src/middleware.ts
- Server-side: The
Security Improvements
The following security improvements have been implemented:
- Removed Insecure Endpoints:
- Deleted
/api/admin/set-cookies- Previously allowed anyone to set admin cookies - Deleted
/api/set-admin-cookies- Another insecure endpoint - Deleted
/api/debug/set-admin-cookies- Third insecure endpoint
- Deleted
- Enhanced Middleware Protection:
- Added specific checks for protected API routes
- Improved error responses for unauthorized access
- Protected Debug Endpoints:
- The
/api/auth/debugendpoint now requires admin authentication - Sensitive information is only shown to authenticated admins
- The
Development and Testing
For development and testing, there are two options:
- Standard Authentication:
- Use the normal login flow through
/admin/login - This is the secure, recommended approach
- Use the normal login flow through
- Development Bypass (controlled environment only):
- Set
BYPASS_AUTH_IN_DEV=truein.env.local - WARNING: Never enable this in production!
- Enabling this creates a security vulnerability
- Set
Best Practices
- Never Create Direct Cookie Endpoints:
- Don't create endpoints that set admin cookies without authentication
- Always verify admin status in the database before granting admin privileges
- Keep Authentication Checks:
- Don't remove or bypass authentication checks
- Don't set
BYPASS_AUTHconstants totruein any files
- Monitor Logs:
- Watch for authentication failures in logs
- Check for suspicious login attempts
Troubleshooting
Issues with admin authentication:
- Make sure your user has
is_admin: truein the database - Use the proper login flow through
/admin/login - Check for any console errors during login
- Verify cookies are being set correctly