@feathersjs/authentication-oauth package provides OAuth 2.0 authentication for various providers like Google, Facebook, GitHub, and more.
Installation
Setup
Configure and register OAuth authentication:Options
string
default:"jwt"
The strategy to use for linking existing accounts
string
The path to the authentication service (defaults to app default)
RequestHandler
Custom Express session middleware
Middleware
Custom Koa session middleware
Configuration
Configure OAuth providers in your authentication settings:string
The URL to redirect to after authentication. If not set, will use the first origin in
originsstring[]
Array of allowed redirect origins for security. Supports multiple origins for different environments
object
Default settings for all OAuth providers:
prefix: URL prefix for OAuth routes (default: ‘/oauth’)origin: Server origin URLtransport: How to send the response (default: ‘state’)response: What to include in response (default: [‘tokens’, ‘raw’, ‘profile’])
object
Configuration for each OAuth provider (e.g., ‘google’, ‘github’):
key: OAuth client IDsecret: OAuth client secretscope: Requested OAuth scopescustom_params: Additional provider-specific parameters
OAuthStrategy
Base class for OAuth authentication strategies.Setup
Register OAuth strategies:Methods
getEntityQuery
Build the query to find an existing entity by OAuth profile. Override to customize.OAuthProfile
required
The OAuth profile from the provider
Params
required
Service call parameters
getEntityData
Get the data to store when creating or updating an entity. Override to customize.OAuthProfile
required
The OAuth profile from the provider
any
required
The existing entity if found, null otherwise
Params
required
Service call parameters
getProfile
Extract the profile from the authentication data. Override to customize.AuthenticationRequest
required
The authentication request data
Params
required
Service call parameters
getRedirect
Get the redirect URL after authentication. Override to customize.AuthenticationResult | Error
required
The authentication result or error
AuthenticationParams
Service call parameters
getCurrentEntity
Get the currently authenticated entity for account linking.Params
required
Service call parameters (should include authentication info)
findEntity
Find an entity by OAuth profile.createEntity
Create a new entity from OAuth profile.updateEntity
Update an existing entity with OAuth profile data.OAuth Flow
The OAuth authentication flow:1. Initiate OAuth Flow
Client redirects to:2. Provider Callback
After user authorizes, provider redirects to:3. Client Receives Token
Client is redirected to the configured redirect URL with the access token:Account Linking
Link an OAuth account to an existing authenticated user:- Authenticate the existing token
- Get the current user
- Update the user with OAuth provider information
- Return a new JWT
Client Usage
Browser
React Example
Custom Redirect Pages
Customize the redirect with query parameters:Complete Server Example
Security
Origin Validation
OAuth redirects are validated against theorigins array to prevent open redirect vulnerabilities:
Redirect Path Validation
Redirect paths are validated to prevent URL authority injection. Paths containing@, \\, or // are rejected.
State Management
OAuth state is managed securely using sessions with CSRF protection.Supported Providers
The package supports any OAuth 2.0 provider through the Grant library. Common providers include:- GitHub
- Microsoft
- Auth0
- And 200+ more