Overview
The Happy Habitat authentication system provides comprehensive user authentication, token management, role selection, and session persistence. The system uses JWT tokens with automatic refresh capabilities.Core Services
AuthService
Location:app/services/auth.service.ts:1
The main authentication service that handles login, registration, and session management.
Key Features:
- User login with username/password
- User registration
- Multi-role support with role selection
- Automatic token refresh
- Session persistence
- Mock authentication mode for development
- Password reset functionality
SessionService
Location:app/services/session.service.ts:1
Manages session storage and token lifecycle.
Key Features:
- Token storage in localStorage
- Token expiration checking
- Session validation
- Secure session cleanup
Authentication Flow
Login Flow
Token Refresh Flow
AuthService API
Login
Multi-Role Login
When a user has multiple roles, the login returns an error with role information:Register
Logout
Token Refresh
Check Authentication
Password Reset
Role-Based Access Control
Role Checking
Available Roles
User Information
Session Management
Storage Keys
Session Storage
Token Expiration
Tokens are considered expired 5 minutes before actual expiration:Reactive State
Signals
The AuthService uses Angular signals for reactive state:Mock Authentication
For development without a backend, enable mock authentication:- Simulates API delay (500ms)
- Creates mock users based on username
- Supports multi-role users (username: ‘elgrandeahc’)
- Generates mock JWT tokens
Auth Guard
Location:app/guards/auth.guard.ts:1
Protects routes from unauthorized access:
Best Practices
1. Use Signals for Reactive State
2. Check Auth on App Initialization
3. Handle Multi-Role Users
4. Sync Auth State
The AuthService synchronizes state with UsersService:Security Considerations
Token Storage
- Tokens are stored in localStorage (not cookies)
- Sensitive headers are redacted in logs
- Tokens are cleared on logout and error
Token Refresh
- Automatic refresh on 401 errors
- 5-minute expiration buffer
- Logout on refresh failure
Session Validation
- Token expiration checked on every auth check
- Invalid sessions automatically cleared
- User redirected to login on expired session