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
Configuration Options
The authentication service accepts the following configuration options:Required Options
| Option | Type | Description |
|---|---|---|
secret | string | The JWT signing secret (keep this secure!) |
entity | string | null | The name of the authentication entity (e.g., ‘user’) |
authStrategies | string[] | Array of strategy names allowed to create JWT tokens |
Optional Options
| Option | Type | Default | Description |
|---|---|---|---|
service | string | - | The path of the entity service (e.g., ‘users’) |
entityId | string | - | The name of the entity id property |
jwtOptions | object | See below | Options for JWT token creation |
parseStrategies | string[] | authStrategies | Strategies that parse HTTP headers |
JWT Options
Authentication Flow
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
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
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