Overview
Nhost supports OAuth 2.0 authentication with various social providers including GitHub, Google, Apple, Discord, and more. This guide explains the OAuth sign-in flow and how to implement it in your application.OAuth Sign-In Flow
The OAuth authentication flow in Nhost involves several steps coordinating between your client application, Nhost Auth service, and the OAuth provider:Implementation Steps
1. Generate the Provider Sign-In URL
Use thesignInProviderURL() method to generate the OAuth authorization URL. This method returns a URL that you’ll redirect the user to:
2. OAuth Provider Authorization
When the user is redirected to the OAuth provider (e.g., GitHub, Google), they will:- See a consent screen asking to authorize your application
- Grant or deny permission to access their profile information
- Be redirected back to Nhost Auth’s callback URL
3. Nhost Auth Callback Processing
Nhost Auth receives the callback from the OAuth provider at/v1/signin/provider/{provider}/callback and performs the following:
- Validates the OAuth state to prevent CSRF attacks
- Exchanges the authorization code for access and refresh tokens from the provider
- Fetches the user’s profile from the provider
- Creates or updates the user in your Nhost database
- Generates a Nhost refresh token for the session
- Redirects to your client application at the
redirectToURL
4. Handle the Redirect
After successful authentication, Nhost redirects back to yourredirectTo URL with query parameters. You need to handle two scenarios:
Success - Extract the Refresh Token
On success, the URL will contain arefreshToken parameter:
Error - Handle Authentication Failure
On error, the URL will contain error parameters:error query parameter:
- User denied authorization at the OAuth provider
- Invalid OAuth request configuration
- Error from the OAuth provider
- Provider account already linked to another user
5. Session Management
Once you’ve exchanged the refresh token for a session, the Nhost SDK automatically manages:- Access token - Short-lived JWT for API requests (default: 15 minutes)
- Refresh token - Used to obtain new access tokens (default: 30 days)
- Automatic token refresh - The SDK refreshes tokens before expiration
Security Considerations
CSRF Protection
Nhost automatically handles CSRF protection using the OAuthstate parameter. Each sign-in request generates a unique state value that is validated during the callback.
Redirect URL Validation
For security, Nhost validates that theredirectTo URL matches either your clientUrl or one of your configured allowed redirect URLs. Configure these in your Nhost project settings.
Custom Domains
To use your own domain for the OAuth callback URL instead of the default Nhost domain, refer to the custom domains documentation.Provider-Specific Setup
Each OAuth provider requires specific configuration. Refer to the provider-specific guides for detailed setup instructions:- Apple
- Azure AD / Entra ID
- Bitbucket
- Discord
- GitHub
- GitLab
- Spotify
- Strava
- Twitch
- Windows Live
- WorkOS
API Reference
For detailed API documentation, see:- signInProviderURL() in the JavaScript SDK reference
- GET /v1/signin/provider/ in the API reference
- GET /v1/signin/provider//callback in the API reference