Skip to main content
The OAuth strategy enables authentication through third-party providers like Google, Facebook, GitHub, and over 200 others. It’s powered by Grant and supports both OAuth 1.0 and OAuth 2.0.

How OAuth Works

The OAuth authentication flow:
  1. User clicks “Login with Provider” - Client redirects to OAuth service
  2. Provider authorization - User authorizes your app on provider’s site
  3. Callback with code - Provider redirects back with authorization code
  4. Token exchange - Server exchanges code for access token
  5. Fetch profile - Server retrieves user profile from provider
  6. Create or link user - Server creates new user or links to existing account
  7. 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
View full list of providers →

Provider Examples

Get credentials:
  1. Go to Google Cloud Console
  2. Create project → Enable Google+ API
  3. Create OAuth 2.0 credentials
  4. Add authorized redirect URI: http://localhost:3030/oauth/google/callback

Client Integration

Redirect to OAuth Provider

With Custom Redirect

Receive Token

After successful authentication, the user is redirected with the token:
Extract the token:

Account Linking

Link OAuth accounts to existing users:
The OAuth strategy will:
  1. Verify the existing JWT token
  2. Get the current user
  3. Link the OAuth account to the user (via updateEntity)
  4. Return a new JWT

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

Always validate redirect origins to prevent open redirect vulnerabilities.
The OAuth strategy validates:
  • 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

Never commit OAuth secrets to version control!

Multi-Tenancy

Scope OAuth authentication to tenants:

Troubleshooting

Redirect URI Mismatch

Solution: Ensure callback URL matches exactly in provider settings:
  • Development: http://localhost:3030/oauth/google/callback
  • Production: https://api.yourdomain.com/oauth/google/callback

Invalid Origin

Solution: Add origin to allowed origins:

Session Not Persisting

Solution: Configure session middleware properly:

Provider Not Configured

Solution: Ensure oauth section exists in authentication config:

Next Steps

JWT Strategy

Understand JWT token authentication

Local Strategy

Add username/password authentication