Skip to main content
The SSO plugin adds Single Sign-On support to Better Auth. It supports OpenID Connect (OIDC), OAuth 2.0, and SAML 2.0 providers, allowing users to authenticate with their corporate identity provider.

Installation

1

Install the package

2

Add the plugin to your auth config

auth.ts
3

Migrate the database

4

Add the client plugin

auth-client.ts

Registering providers

Register an OIDC provider

Better Auth automatically fetches the provider’s OIDC discovery document, so most endpoint fields are optional:
The redirect callback URL is automatically generated as:
Discovery endpoints must be in your trustedOrigins configuration. Add the IdP origin: trustedOrigins: ["https://your-org.okta.com"]

Register a SAML provider

Sign in with SSO

Provisioning

User provisioning

Run custom logic whenever a user signs in through SSO:
auth.ts

Organization provisioning

Automatically add SSO users to organizations:
auth.ts

Linking providers to organizations

Users from acmecorp.com signing in through this provider are automatically added to the Acme Corp organization.

SAML endpoints

The plugin creates these endpoints automatically:

Get SP metadata

SAML security

InResponseTo validation

Prevent replay attacks and unsolicited responses:
auth.ts

Timestamp validation

auth.ts

Algorithm validation

auth.ts
Replay protection (assertion ID deduplication) is always enabled and cannot be disabled.

OIDC discovery

Better Auth automatically fetches the IdP’s discovery document from {issuer}/.well-known/openid-configuration. The following fields are auto-discovered:
  • authorizationEndpoint
  • tokenEndpoint
  • jwksEndpoint
  • userInfoEndpoint
  • discoveryEndpoint
  • tokenEndpointAuthentication
Explicitly set fields always override discovered values.

Discovery errors

Domain verification

Enable domain verification to require ownership proof before trusting SSO providers:
auth.ts
Verification flow:
1
Register the provider — a verification token is returned in the response.
2
Add a DNS TXT record: _better-auth-token-{providerId} with the token value.
3
Submit a verification request:
Verified domains also enable automatic account linking: if a user signs in through an SSO provider and an account with the same email already exists, the accounts are linked automatically.

Schema

The plugin creates an ssoProvider table: