OAuth
Better Auth comes with built-in support for OAuth 2.0 and OpenID Connect. This allows you to authenticate users via popular OAuth providers like Google, Facebook, GitHub, and more.
If your desired provider isn’t directly supported, you can use the Generic OAuth Plugin for custom integrations.
Configuring Social Providers
To enable a social provider, you need to provide clientId
and clientSecret
for the provider.
Here’s an example of how to configure Google as a provider:
Other Provider Configurations
scope The scope of the access request. For example, email
or profile
.
redirectURI Custom redirect URI for the provider. By default, it uses /api/auth/callback/${providerName}
.
disableIdTokenSignIn: Disables the use of the ID token for sign-in. By default, it’s enabled for some providers like Google and Apple.
verifyIdToken A custom function to verify the ID token.
getUserInfo A custom function to fetch user information from the provider. Given the tokens returned from the provider, this function should return the user’s information.
mapProfileToUser A custom function to map the user profile returned from the provider to the user object in your database.
Useful, if you have additional fields in your user object you want to populate from the provider’s profile. Or if you want to change how by default the user object is mapped.
How OAuth Works in Better Auth
Here’s what happens when a user selects a provider to authenticate with:
- Configuration Check: Ensure the necessary provider details (e.g., client ID, secret) are configured.
- State Generation: Generate and save a state token in your database for CSRF protection.
- PKCE Support: If applicable, create a PKCE code challenge and verifier for secure exchanges.
- Authorization URL Construction: Build the provider’s authorization URL with parameters like client ID, redirect URI, state, etc. The callback URL usually follows the pattern
/api/auth/callback/${providerName}
. - User Redirection:
- If redirection is enabled, users are redirected to the provider’s login page.
- If redirection is disabled, the authorization URL is returned for the client to handle the redirection.
Post-Login Flow
After the user completes the login process, the provider redirects them back to the callback URL with a code and state. Better Auth handles the rest:
- Token Exchange: The code is exchanged for an access token and user information.
- User Handling:
- If the user doesn’t exist, a new account is created.
- If the user exists, they are logged in.
- If the user has multiple accounts across providers, Better Auth links them based on your configuration. Learn more about account linking.
- Session Creation: A new session is created for the user.
- Redirect: Users are redirected to the specified URL provided during the initial request or
/
.
If any error occurs during the process, Better Auth handles it and redirects the user to the error URL (if provided) or the callbackURL. And it includes the error message in the query string ?error=...
.