How OAuth Works
The OAuth authentication flow:- User clicks “Login with Provider” - Client redirects to OAuth service
- Provider authorization - User authorizes your app on provider’s site
- Callback with code - Provider redirects back with authorization code
- Token exchange - Server exchanges code for access token
- Fetch profile - Server retrieves user profile from provider
- Create or link user - Server creates new user or links to existing account
- Return JWT - Server creates JWT and redirects with access token
Installation
Basic Setup
1
Install Dependencies
Install required packages:
2
Configure OAuth
Add OAuth configuration for your providers:
3
Register Strategy and Service
Set up OAuth strategy and service:
4
Update User Service
Add provider ID fields to your user schema:
Configuration Options
OAuth Settings
Provider Settings
Each provider (e.g.,google, github) can have:
Strategy Options
Supported Providers
Over 200 providers are supported via Grant. Common examples:- Google -
google - Facebook -
facebook - GitHub -
github - Twitter -
twitter - Microsoft -
microsoft - LinkedIn -
linkedin - Apple -
apple - Discord -
discord - Twitch -
twitch - Spotify -
spotify
Provider Examples
- Google
- GitHub
- Facebook
- Apple
- Go to Google Cloud Console
- Create project → Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:3030/oauth/google/callback
Client Integration
Redirect to OAuth Provider
With Custom Redirect
Popup Window
Receive Token
After successful authentication, the user is redirected with the token:Account Linking
Link OAuth accounts to existing users:Link via JWT
- Verify the existing JWT token
- Get the current user
- Link the OAuth account to the user (via
updateEntity) - Return a new JWT
Link in Custom Strategy
Customization
Custom Profile Data
Extract additional data from OAuth profile:Custom Entity Query
Customize how users are found:Create vs Update Logic
Prevent User Creation
Security Configuration
Allowed Origins
- Referer header matches allowed origin
- Redirect parameter doesn’t contain URL injection characters
- Final redirect URL starts with allowed origin
Redirect Validation
Session Security
Configure secure session handling:Environment Variables
Multi-Tenancy
Scope OAuth authentication to tenants:Troubleshooting
Redirect URI Mismatch
- Development:
http://localhost:3030/oauth/google/callback - Production:
https://api.yourdomain.com/oauth/google/callback
Invalid Origin
Session Not Persisting
Provider Not Configured
oauth section exists in authentication config:
Next Steps
JWT Strategy
Understand JWT token authentication
Local Strategy
Add username/password authentication