3.4 KiB
3.4 KiB
Development Authentication Setup
Quick Fix for Sign-In Issues
Your authentication system has been updated to be more development-friendly. Here are the changes made:
1. CSRF Token Handling Fixed
- Frontend: Added automatic CSRF token fetching and inclusion in API requests
- Backend: Added development bypass for CSRF protection
- CORS: Added
X-CSRF-Tokento allowed headers
2. Development Environment Variables
Add these to your .env file to simplify development:
# Disable CSRF protection for easier development
DISABLE_CSRF=true
# Disable rate limiting for development
DISABLE_RATE_LIMIT=true
# Disable account locking for development
DISABLE_ACCOUNT_LOCKING=true
# Enable detailed error messages
EXPOSE_VALIDATION_ERRORS=true
# CORS configuration
CORS_ORIGIN=http://localhost:3000
3. What Was Complex About Your Auth System
Your authentication system had several layers of security that, while production-ready, made development difficult:
- CSRF Protection: Double-submit cookie pattern with session/user binding
- Rate Limiting: 5 login attempts per 15 minutes per IP+UA
- Account Locking: Exponential backoff on failed login attempts
- Extensive Audit Logging: Every auth event logged with full context
- Multiple Auth Strategies: JWT + Local + Session management
- Complex Error Handling: Secure error mapping to prevent information leakage
4. Simplified Development Flow
With the new configuration:
- CSRF tokens are automatically handled by the frontend API client
- Development bypasses can be enabled via environment variables
- Better error messages in development mode
- Exempt paths include all necessary auth endpoints
5. Production Security Maintained
All security features remain intact for production:
- Set
DISABLE_CSRF=falseor remove the variable - Set
DISABLE_RATE_LIMIT=falseor remove the variable - Set
DISABLE_ACCOUNT_LOCKING=falseor remove the variable
6. Testing the Fix
- Restart your backend server
- Try logging in through the frontend
- Check the browser network tab to see requests going to
/api/auth/login - Check server logs for CSRF bypass messages (if debug enabled)
6.1. Core API Configuration Fix - The Real Issue
Root Cause: The frontend code was inconsistent with OpenAPI generated types.
- OpenAPI Types: Expect paths like
/api/auth/login(with/apiprefix) - Frontend Code: Was calling paths like
/auth/login(without/apiprefix) - Workaround:
normalizePathfunction was adding/apiprefix automatically
Proper Fix Applied:
- Removed Path Normalization: Eliminated the
normalizePathfunction entirely - Fixed Frontend Calls: Updated all API calls to use correct OpenAPI paths with
/apiprefix - Base URL Correction: Set base URL to
http://localhost:4000(without/api) - Router Configuration: Added SecurityModule to API routes
Result: Clean, consistent API calls that match the OpenAPI specification exactly.
7. If Issues Persist
Check these common issues:
- CORS Configuration: Ensure
CORS_ORIGINmatches your frontend URL - Cookie Settings: Ensure cookies are being set and sent properly
- Network Tab: Check if CSRF token requests are successful
- Server Logs: Look for detailed error messages with the new logging
The authentication system is now much more developer-friendly while maintaining production security standards.