Skip to main content
The @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

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 origins
string[]
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 URL
  • transport: 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 ID
  • secret: OAuth client secret
  • scope: Requested OAuth scopes
  • custom_params: Additional provider-specific parameters
Example:

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
Default behavior:
Example:

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
Default behavior:
Example:

getProfile

Extract the profile from the authentication data. Override to customize.
AuthenticationRequest
required
The authentication request data
Params
required
Service call parameters
Example:

getRedirect

Get the redirect URL after authentication. Override to customize.
AuthenticationResult | Error
required
The authentication result or error
AuthenticationParams
Service call parameters
Example:

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:
Example:

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:
Or with an error:

Account Linking

Link an OAuth account to an existing authenticated user:
The OAuth strategy will:
  1. Authenticate the existing token
  2. Get the current user
  3. Update the user with OAuth provider information
  4. 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 the origins 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:
  • Google
  • GitHub
  • Facebook
  • Twitter
  • LinkedIn
  • Microsoft
  • Auth0
  • And 200+ more
See the Grant documentation for the complete list and provider-specific configuration.