Authentication Flow
User Registration
Users register with their email and password. The system generates a 6-digit verification code that expires in 15 minutes.
Email Verification
Users verify their email using the 6-digit code. Upon successful verification, they receive authentication tokens.
User Login
Verified users can log in with their email and password.The login process validates credentials and checks email verification status:
Token Refresh Mechanism
When the access token expires, use the refresh token to obtain a new pair of tokens:JWT Strategy
The JWT strategy validates access tokens and loads user data:Security Features
Rate Limiting
All authentication endpoints use@UseGuards(ThrottlerGuard) to prevent brute force attacks:
HttpOnly Cookies
Refresh tokens are stored in secure HttpOnly cookies:Password Hashing
Passwords are hashed using bcrypt with 12 rounds:Resending Verification Code
If the verification code expires or is lost, users can request a new one:Getting Current User
Protected endpoints can access the authenticated user:Logout
Logging out clears the refresh token cookie:Best Practices
Token Expiration
Token Expiration
- Access tokens expire in 15 minutes to minimize exposure
- Refresh tokens expire in 7 days to balance security and UX
- Expired verification codes trigger account deletion for security
Environment Variables
Environment Variables
Required environment variables:
JWT_ACCESS_SECRET: Secret for access tokensJWT_REFRESH_SECRET: Secret for refresh tokens (must be different)NODE_ENV: Set to ‘production’ for HTTPS-only cookies
Error Handling
Error Handling
- Always use generic error messages for failed authentication
- Log detailed errors server-side for debugging
- Never expose password hashes or internal details
