Core Architecture
The authentication system is built around theAuthenticationService which manages:
- Authentication strategies - Pluggable methods for authenticating users (JWT, local, OAuth)
- JWT token management - Creating and verifying access tokens
- Real-time connection handling - Managing authenticated WebSocket connections
- HTTP request parsing - Extracting authentication from HTTP headers
Installation
Basic Setup
1
Configure Authentication
Set up authentication in your application configuration:
2
Create Authentication Service
Register the authentication service in your application:
3
Protect Routes
Use the
authenticate hook to protect your services:Configuration Options
The authentication service accepts the following configuration options:Required Options
Optional Options
JWT Options
Authentication Flow
1
Client Login Request
Client sends credentials to the authentication service:
2
Strategy Validation
The authentication service:
- Validates the strategy is allowed (in
authStrategies) - Calls the strategy’s
authenticate()method - Strategy validates credentials and returns user entity
3
JWT Creation
The service creates a JWT access token:
- Calls
getPayload()to build token payload - Calls
getTokenOptions()to set token options (subject, expiration) - Signs the token with the configured secret
4
Return Authentication Result
Returns the authentication result:
Using the authenticate Hook
Theauthenticate hook protects service methods by requiring authentication:
- Single Strategy
- Multiple Strategies
- With Options
Accessing Authenticated User
After successful authentication, the user entity is available inparams:
Real-time Connection Management
The authentication system automatically manages WebSocket connections:Security Best Practices
Recommendations
- Use strong secrets - Generate a cryptographically secure random string (at least 32 characters)
- Set appropriate token expiration - Balance security and user experience (1d for web, 30d for mobile)
- Use HTTPS in production - Always transmit tokens over secure connections
- Rotate secrets periodically - Implement a key rotation strategy
- Validate token audience - Ensure tokens are used for the intended application
- Use refresh tokens - Implement refresh token rotation for long-lived sessions
Custom Token Payload
Extend thegetPayload method to add custom claims:
Custom Token Options
Customize token options per request:Logging Out
Remove authentication (triggerslogout event):
Events
The authentication service emits events during the authentication lifecycle:login- After successful authenticationlogout- After successful logoutdisconnect- When a real-time connection closes
Next Steps
JWT Strategy
Learn about JWT token authentication
Local Strategy
Implement username/password authentication
OAuth Strategy
Add social login with OAuth providers