Overview
Ceboelha API uses a robust JWT-based authentication system with the following security features:- Dual-token system: Short-lived access tokens (15 minutes) and long-lived refresh tokens (7 days)
- Token rotation: Refresh tokens are invalidated after use to prevent token theft
- Account lockout: Automatic lockout after 5 failed login attempts
- Secure storage: Tokens stored as SHA-256 hashes, never in plain text
- Device tracking: Session management with device and IP tracking
- HttpOnly cookies: Tokens sent via secure httpOnly cookies for XSS protection
Authentication Flow
Registration
Create a new user account:- Minimum 8 characters
- At least 1 uppercase letter
- At least 1 lowercase letter
- At least 1 number
- At least 1 special character (
!@#$%^&*...)
auth.service.ts:179-182 for password validation logic.
Response:
Tokens are automatically set as httpOnly cookies in the
Set-Cookie header. The expiresIn value (in seconds) helps clients know when to refresh.Login
Authenticate with email and password:Refresh Token
Access tokens expire after 15 minutes. Use the refresh token to get a new access token:auth.service.ts:407-411).
If a revoked token is reused, the system detects potential token theft and revokes ALL tokens for that user as a security measure (
auth.service.ts:386-392).Logout
Revoke tokens to logout:The refresh token to revoke. If omitted, only the current session is invalidated.
Set to
true to logout from all devices by revoking all refresh tokens.Token Structure
Access Token (JWT)
Access tokens are signed JWTs with the following payload:User ID (MongoDB ObjectId)
User email address
User role:
"user" or "admin"Token type: always
"access"Issued at timestamp (Unix epoch)
Expiration timestamp (Unix epoch)
Issuer:
"ceboelha-api"Audience:
"ceboelha-app"auth.service.ts:109-124):
Refresh Token
Refresh tokens are cryptographically secure random strings (64 bytes = 128 hex characters), not JWTs. They are stored in the database as SHA-256 hashes with:- User ID reference
- Device information (User-Agent, IP)
- Expiration date (7 days)
- Revocation status and reason
refresh-token.model.ts:139-158 for token generation.
Making Authenticated Requests
Using Authorization Header
Include the access token in theAuthorization header:
Using HttpOnly Cookies (Recommended)
If tokens are set via cookies, they’re automatically sent:auth.middleware.ts:48-58) checks both sources:
Session Management
List Active Sessions
Get all active sessions (logged-in devices):Revoke a Session
Remotely logout a specific device:Security Features
Account Lockout
Protection against brute force attacks (login-attempt.model.ts:130-162):
- After 5 failed login attempts, the account is locked for 15 minutes
- Failed attempts are tracked per email address
- Successful login resets the counter
- Users receive warnings after the 3rd failed attempt
env.ts:59-60):
Token Storage Security
Refresh tokens are never stored in plain text:Device Tracking
Every login and token refresh records:- IP address (from
X-Forwarded-FororX-Real-IPheaders) - User-Agent string
- Timestamp
- Suspicious activity detection
- Session management
- Security auditing
auth.middleware.ts:152-159 for device info extraction.
Token Theft Detection
If a revoked refresh token is used, the system assumes token theft and:- Revokes ALL refresh tokens for that user
- Forces logout on all devices
- User must re-authenticate everywhere
Environment Configuration
Configure JWT settings via environment variables:env.ts:20-27 for secret validation (minimum 32 characters enforced).
Error Handling
Common authentication errors:| Error Code | Status | Description |
|---|---|---|
UNAUTHORIZED | 401 | Invalid or expired token |
FORBIDDEN | 403 | Valid token but insufficient permissions |
RATE_LIMIT | 429 | Account locked due to failed attempts |
VALIDATION_ERROR | 400 | Weak password or invalid input |
CONFLICT | 409 | Email already registered |