# Single Sign-On (SSO) (/docs/plugins/sso)

Connect each organization to its own OIDC or SAML 2.0 identity provider.



The SSO plugin lets your users sign in through identity providers that you or your customers register at runtime, over [OpenID Connect](https://openid.net/specs/openid-connect-core-1_0.html) or [SAML 2.0](https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf). Each registered connection is one row in the `ssoProvider` table. You can also [define connections in code](#configure-providers-in-code).

For a fixed set of providers configured in code, use [Generic OAuth](/docs/plugins/generic-oauth) or [social providers](/docs/concepts/oauth). To let other apps sign in with yours, use [OAuth Provider](/docs/plugins/oauth-provider).

<Callout>
  Self-service SSO, where customers configure their own connection through an onboarding link, is available with [Better Auth Infrastructure](https://dash.better-auth.com/sign-in). For other enterprise needs, [contact us for enterprise](/enterprise).
</Callout>

## Features [#features]

* [OIDC discovery](/docs/plugins/sso/oidc#oidc-discovery) of provider endpoints
* [`private_key_jwt`](/docs/plugins/sso/oidc#token-endpoint-authentication) token endpoint authentication
* SAML 2.0 [SP-initiated](/docs/plugins/sso/saml#register-a-saml-provider) and [IdP-initiated](/docs/plugins/sso/saml#idp-initiated-sso) sign-in
* [SAML response validation](/docs/plugins/sso/saml-security): signatures, algorithms, timestamps, audience, and replay
* SAML [Single Logout](/docs/plugins/sso/saml#single-logout)
* [User resolution](/docs/plugins/sso/provisioning#resolve-sso-users) and provisioning
* [Organization provisioning](/docs/plugins/sso/provisioning#organization-provisioning)
* DNS [domain verification](/docs/plugins/sso/domain-verification#enable-domain-verification)

## Installation [#installation]

<Steps>
  <Step>
    ### Install the plugin [#install-the-plugin]

    <CodeBlockTabs defaultValue="npm" groupId="persist-install">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="npm">
          npm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="pnpm">
          pnpm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="yarn">
          yarn
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="bun">
          bun
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="npm">
        ```bash
        npm install @better-auth/sso
        ```
      </CodeBlockTab>

      <CodeBlockTab value="pnpm">
        ```bash
        pnpm add @better-auth/sso
        ```
      </CodeBlockTab>

      <CodeBlockTab value="yarn">
        ```bash
        yarn add @better-auth/sso
        ```
      </CodeBlockTab>

      <CodeBlockTab value="bun">
        ```bash
        bun add @better-auth/sso
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Step>

  <Step>
    ### Add the plugin to the server [#add-the-plugin-to-the-server]

    ```ts title="auth.ts"
    import { betterAuth } from "better-auth";
    import { sso } from "@better-auth/sso"; // [!code highlight]

    export const auth = betterAuth({
      plugins: [
        sso(), // [!code highlight]
      ],
    });
    ```
  </Step>

  <Step>
    ### Migrate the database [#migrate-the-database]

    Run the migration or generate the schema to add the necessary fields and tables to the database.

    <Tabs items="[&#x22;migrate&#x22;, &#x22;generate&#x22;]">
      <Tab value="migrate">
        <CodeBlockTabs defaultValue="npm" groupId="persist-install">
          <CodeBlockTabsList>
            <CodeBlockTabsTrigger value="npm">
              npm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="pnpm">
              pnpm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="yarn">
              yarn
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="bun">
              bun
            </CodeBlockTabsTrigger>
          </CodeBlockTabsList>

          <CodeBlockTab value="npm">
            ```bash
            npx auth migrate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="pnpm">
            ```bash
            pnpm dlx auth migrate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="yarn">
            ```bash
            yarn dlx auth migrate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="bun">
            ```bash
            bun x auth migrate
            ```
          </CodeBlockTab>
        </CodeBlockTabs>
      </Tab>

      <Tab value="generate">
        <CodeBlockTabs defaultValue="npm" groupId="persist-install">
          <CodeBlockTabsList>
            <CodeBlockTabsTrigger value="npm">
              npm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="pnpm">
              pnpm
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="yarn">
              yarn
            </CodeBlockTabsTrigger>

            <CodeBlockTabsTrigger value="bun">
              bun
            </CodeBlockTabsTrigger>
          </CodeBlockTabsList>

          <CodeBlockTab value="npm">
            ```bash
            npx auth generate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="pnpm">
            ```bash
            pnpm dlx auth generate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="yarn">
            ```bash
            yarn dlx auth generate
            ```
          </CodeBlockTab>

          <CodeBlockTab value="bun">
            ```bash
            bun x auth generate
            ```
          </CodeBlockTab>
        </CodeBlockTabs>
      </Tab>
    </Tabs>

    See the [Schema](/docs/plugins/sso/reference#schema) section to add the fields manually.
  </Step>

  <Step>
    ### Add the client plugin [#add-the-client-plugin]

    `ssoClient()` takes the same `domainVerification` and `schema.ssoProvider.additionalFields` options as the server plugin, so the client types match.

    ```ts title="auth-client.ts"
    import { createAuthClient } from "better-auth/client";
    import { ssoClient } from "@better-auth/sso/client"; // [!code highlight]

    export const authClient = createAuthClient({
      plugins: [
        ssoClient(), // [!code highlight]
      ],
    });
    ```
  </Step>
</Steps>

## Register a provider [#register-a-provider]

By default, any signed-in user can register up to 10 providers. Before you deploy, decide who should register providers:

| Who registers providers                   | Configure                                                                                                                                                                                                                                                           |
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Only your administrators                  | Set `providersLimit` to a function that returns `0` for everyone else. Owners and admins of an organization can still update its linked providers, including their `domain`; block that with [`guardProviderMutation`](/docs/plugins/sso/reference#plugin-options). |
| Your customers, each for their own domain | Enable [domain verification](/docs/plugins/sso/domain-verification). A provider then cannot sign anyone in until its domain is proven over DNS.                                                                                                                     |
| Nobody at runtime                         | Set `providersLimit: 0` and define the providers in [`defaultSSO`](#configure-providers-in-code). To link existing users by email, also enable domain verification.                                                                                                 |

As a function, `providersLimit` receives the signed-in user and returns the number of providers that user can register. A limit of `0` rejects that user's registrations with 403.

```ts title="auth.ts"
sso({
  providersLimit: async (user) => ((await isAdmin(user.id)) ? 10 : 0), // your check
})
```

To register a provider, call `authClient.sso.register`. It calls `POST /sso/register` and creates a provider owned by the signed-in user. On the server, call `auth.api.registerSSOProvider` with the request's headers. Every registration needs:

* `providerId`: unique. The IDs of built-in providers, your configured social providers, `account.accountLinking.trustedProviders` entries, and `defaultSSO` entries are rejected with 422.
* `issuer`: the IdP issuer URL for OIDC, or your SP entity ID for SAML.
* `domain`: a bare email domain such as `example.com`. Separate several domains with commas.
* `oidcConfig`, `samlConfig`, or both.

To register an OIDC provider that uses discovery, first add the IdP's origin to `trustedOrigins`. Registration fetches the IdP's discovery document and fails with `discovery_untrusted_origin` when the origin is missing. `trustedOrigins` applies to the whole app, so list only IdP origins you have vetted. With `skipDiscovery: true` and every endpoint set in `oidcConfig`, registration fetches nothing. See [Trusted origins](/docs/plugins/sso/oidc#trusted-origins).

The response includes `redirectURI`, and `domainVerificationToken` when domain verification is enabled.

```ts title="register-provider.ts"
import { authClient } from "@/lib/auth-client";

const { data, error } = await authClient.sso.register({
  providerId: "acme",
  issuer: "https://idp.example.com",
  domain: "example.com",
  oidcConfig: {
    clientId: "CLIENT_ID",
    clientSecret: "CLIENT_SECRET",
  },
});

if (error) {
  // Show error.message. Discovery failures set error.code, such as discovery_untrusted_origin.
} else {
  // Register data.redirectURI as the redirect URI at the IdP.
}
```

For every field, see [Register an OIDC provider](/docs/plugins/sso/oidc#register-an-oidc-provider) and [Register a SAML provider](/docs/plugins/sso/saml#register-a-saml-provider).

<Callout type="warn">
  Passing `organizationId` requires organization membership. See [Link a provider to an organization](/docs/plugins/sso/provisioning#link-a-provider-to-an-organization).
</Callout>

### Configure providers in code [#configure-providers-in-code]

To keep providers in your config instead of the database, pass `defaultSSO`, an array of `{ providerId, domain, oidcConfig?, samlConfig?, privateKey? }` entries. Better Auth checks them before database providers. List only domains the IdP is authoritative for.

Whether a sign-in through these providers links to an existing user depends on domain verification:

* With `domainVerification.enabled: true`, they count as verified without a DNS check. A sign-in can link to an existing user at a listed domain when the other [linking conditions](/docs/plugins/sso/provisioning#how-sso-users-are-matched) hold.
* Without it, Better Auth does not link them by email. An existing user's first SSO sign-in then fails unless [`resolveUser`](/docs/plugins/sso/provisioning#resolve-sso-users) links it.

```ts title="auth.ts"
sso({
  defaultSSO: [
    {
      providerId: "acme-saml",
      domain: "example.com",
      samlConfig: {
        issuer: "https://app.example.com",
        entryPoint: "https://idp.example.com/sso",
        idpMetadata: {
          entityID: "https://idp.example.com",
          cert: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
        },
      },
    },
  ],
})
```

To use [`private_key_jwt`](/docs/plugins/sso/oidc#token-endpoint-authentication) without storing the key in the database, set `privateKey` (`{ privateKeyJwk?, privateKeyPem? }`) on the entry.

## Sign in [#sign-in]

To sign in with an SSO provider, call `signIn.sso`.

```ts title="sign-in.ts"
import { authClient } from "@/lib/auth-client";

await authClient.signIn.sso({
  email: "user@example.com",
  callbackURL: "/dashboard",
});
```

In the browser, the auth client sends the user to the IdP by itself. On the server, call `auth.api.signInSSO`. It returns the IdP's `url`, and your code must send the browser there.

<Callout type="warn">
  `auth.api.signInSSO` does not check `callbackURL`, `errorCallbackURL`, or `newUserCallbackURL` against [`trustedOrigins`](/docs/reference/security#trusted-origins). Only requests through `auth.handler` get that check. If your server code passes a URL from the request, validate it first, or the sign-in becomes an open redirect.
</Callout>

Pass the identifier you have:

* The user's email, as `email`, or their email domain, as `domain`.
* The organization's slug, as `organizationSlug`.
* The provider's ID, as `providerId`.

To choose the provider, Better Auth tries these in order:

1. If you pass `providerId`, it uses the provider with that ID. No other field is used.
2. If a `defaultSSO` entry's domains include `domain`, or the domain part of `email`, it uses that entry.
3. If you pass `organizationSlug`, it uses the provider linked to that organization. This needs the [organization plugin](/docs/plugins/organization). An unknown slug continues to the next step. A known organization without a linked provider returns 404.
4. Otherwise, it uses a registered provider for that domain.

With domain verification enabled, a provider whose domain is not verified returns 401.

Other sign-in fields:

* `loginHint` is sent to OIDC providers as [`login_hint`](https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest), and falls back to `email`. SAML ignores it.
* `providerType` picks the protocol when a provider has both configurations, and OIDC is the default. Requesting a protocol the provider does not have returns 400.
* `additionalParams` adds parameters to the OIDC authorization request. Reserved parameters such as `state`, `client_id`, and `redirect_uri` are rejected, and SAML providers reject it with 400.
* To create a new user even when `disableImplicitSignUp` is on, pass `requestSignUp: true`. Any caller can set it, and SAML ignores it. To refuse users you have not provisioned, see [Block new users](/docs/plugins/sso/provisioning#block-new-users).

**Endpoint:** `POST /sign-in/sso`

### Client Side

```ts
const { data, error } = await authClient.signIn.sso({
    email: "user@example.com", // Email address whose domain identifies the provider.
    organizationSlug: "example-org", // Slug of the organization whose provider to use.
    providerId: "acme", // ID of the provider to use.
    domain: "example.com", // Email domain of the provider.
    callbackURL: "/dashboard", // required, URL to redirect to after a successful sign-in.
    errorCallbackURL: "/sign-in/error", // URL to redirect to if sign-in fails.
    newUserCallbackURL: "/welcome", // URL to redirect to when the user is new.
    scopes: ["openid", "email", "profile", "offline_access"], // OIDC scopes to request. Defaults to the provider's scopes, then `["openid", "email", "profile", "offline_access"]`.
    loginHint: "user@example.com", // Sent to OIDC providers as `login_hint`.
    additionalParams, // Extra OIDC authorization request parameters. Rejected for SAML.
    requestSignUp: true, // Create a new user even when `disableImplicitSignUp` is on. OIDC only.
    providerType: "oidc", // Protocol to use when the provider has both configurations. Defaults to `oidc`.
});
```

### Server Side

```ts
const data = await auth.api.signInSSO({
    body: {
        email: "user@example.com", // Email address whose domain identifies the provider.
        organizationSlug: "example-org", // Slug of the organization whose provider to use.
        providerId: "acme", // ID of the provider to use.
        domain: "example.com", // Email domain of the provider.
        callbackURL: "/dashboard", // required, URL to redirect to after a successful sign-in.
        errorCallbackURL: "/sign-in/error", // URL to redirect to if sign-in fails.
        newUserCallbackURL: "/welcome", // URL to redirect to when the user is new.
        scopes: ["openid", "email", "profile", "offline_access"], // OIDC scopes to request. Defaults to the provider's scopes, then `["openid", "email", "profile", "offline_access"]`.
        loginHint: "user@example.com", // Sent to OIDC providers as `login_hint`.
        additionalParams, // Extra OIDC authorization request parameters. Rejected for SAML.
        requestSignUp: true, // Create a new user even when `disableImplicitSignUp` is on. OIDC only.
        providerType: "oidc", // Protocol to use when the provider has both configurations. Defaults to `oidc`.
    },
});
```

### Type Definition

```ts
type signInSSO = {
    /**
     * Email address whose domain identifies the provider.
     */
    email?: string = "user@example.com"
    /**
     * Slug of the organization whose provider to use.
     */
    organizationSlug?: string = "example-org"
    /**
     * ID of the provider to use.
     */
    providerId?: string = "acme"
    /**
     * Email domain of the provider.
     */
    domain?: string = "example.com"
    /**
     * URL to redirect to after a successful sign-in.
     */
    callbackURL: string = "/dashboard"
    /**
     * URL to redirect to if sign-in fails.
     */
    errorCallbackURL?: string = "/sign-in/error"
    /**
     * URL to redirect to when the user is new.
     */
    newUserCallbackURL?: string = "/welcome"
    /**
     * OIDC scopes to request. Defaults to the provider's scopes, then `["openid", "email", "profile", "offline_access"]`.
     */
    scopes?: string[] = ["openid", "email", "profile", "offline_access"]
    /**
     * Sent to OIDC providers as `login_hint`.
     */
    loginHint?: string = "user@example.com"
    /**
     * Extra OIDC authorization request parameters. Rejected for SAML.
     */
    additionalParams?: Record<string, string>
    /**
     * Create a new user even when `disableImplicitSignUp` is on. OIDC only.
     */
    requestSignUp?: boolean = true
    /**
     * Protocol to use when the provider has both configurations. Defaults to `oidc`.
     */
    providerType?: "oidc" | "saml" = "oidc"
}
```

## Manage providers [#manage-providers]

To list the providers you can manage, call `authClient.sso.providers()` (`GET /sso/providers`). It returns the providers the signed-in user registered without an organization, plus the providers of organizations where that user is an `owner` or `admin`. To fetch one, call `authClient.sso.getProvider({ query: { providerId } })` (`GET /sso/get-provider`). Both return a sanitized configuration: `clientIdLastFour` instead of the client ID, no secrets, and the parsed SAML signing certificates. You need the same access to update or delete a provider.

**Endpoint:** `POST /sso/update-provider`

### Client Side

```ts
const { data, error } = await authClient.sso.updateProvider({
    providerId: "acme", // required, ID of the provider to update.
    issuer: "https://idp.example.com", // New issuer URL.
    domain: "example.com", // New email domain or comma-separated domains.
    oidcConfig: { clientSecret: "NEW_CLIENT_SECRET" }, // Fields of the OIDC configuration to change.
    samlConfig, // Fields of the SAML configuration to change. Set `idpInitiatedCallbackUrl` to `null` to clear it.
});
```

### Server Side

```ts
const data = await auth.api.updateSSOProvider({
    body: {
        providerId: "acme", // required, ID of the provider to update.
        issuer: "https://idp.example.com", // New issuer URL.
        domain: "example.com", // New email domain or comma-separated domains.
        oidcConfig: { clientSecret: "NEW_CLIENT_SECRET" }, // Fields of the OIDC configuration to change.
        samlConfig, // Fields of the SAML configuration to change. Set `idpInitiatedCallbackUrl` to `null` to clear it.
    },
    // This endpoint requires session cookies.
    headers: await headers(),
});
```

### Type Definition

```ts
type updateSSOProvider = {
    /**
     * ID of the provider to update.
     */
    providerId: string = "acme"
    /**
     * New issuer URL.
     */
    issuer?: string = "https://idp.example.com"
    /**
     * New email domain or comma-separated domains.
     */
    domain?: string = "example.com"
    /**
     * Fields of the OIDC configuration to change.
     */
    oidcConfig?: Partial<OIDCConfig> = { clientSecret: "NEW_CLIENT_SECRET" }
    /**
     * Fields of the SAML configuration to change. Set `idpInitiatedCallbackUrl` to `null` to clear it.
     */
    samlConfig?: Partial<SAMLConfig>
}
```

* A supplied `mapping` replaces the whole previous mapping.
* Changing `domain` resets `domainVerified` to `false`.
* Changing an identity field returns 409 while accounts are linked to the provider. The identity fields are:
  * Both: `issuer`
  * OIDC: authorization, token, userinfo, JWKS, and discovery endpoints, and `clientId`
  * SAML: IdP entity ID, IdP SSO URLs, SP entity ID, NameID format, POST ACS URLs, `wantAssertionsSigned`, and `audience`

**Endpoint:** `POST /sso/delete-provider`

### Client Side

```ts
const { data, error } = await authClient.sso.deleteProvider({
    providerId: "acme", // required, ID of the provider to delete.
});
```

### Server Side

```ts
const data = await auth.api.deleteSSOProvider({
    body: {
        providerId: "acme", // required, ID of the provider to delete.
    },
    // This endpoint requires session cookies.
    headers: await headers(),
});
```

### Type Definition

```ts
type deleteSSOProvider = {
    /**
     * ID of the provider to delete.
     */
    providerId: string = "acme"
}
```

<Callout type="warn">
  Deleting a provider also deletes every `account` row linked to it. It does not end sessions that users started through the provider, or remove organization memberships it provisioned. If you delete a provider because its IdP was compromised, also revoke those users' sessions.
</Callout>

To block an update or delete of a stored provider, throw from [`guardProviderMutation`](/docs/plugins/sso/reference#plugin-options). The change is rejected with 409 `SSO_PROVIDER_MUTATION_REJECTED`. The guard runs after the provider row is locked, and it needs a database adapter with native transactions and async-context support. It receives:

* `action`: `"update"` or `"delete"`.
* `provider`: the `id`, `providerId`, and `organizationId` of the stored row.
* `providerReference`: an opaque reference to that row's configuration. Never persist it.
* `isAuthenticationBoundaryChange`, on update only. It is `true` for the identity fields above and for SAML signing certificate changes. It is `false` for OIDC client secret and SAML SP private key rotation.

Read related state through the `database` in the guard's second argument, because it runs in the same transaction. For example, reject boundary changes while a directory sync is paired. To change the IdP trust configuration, unpair the directory, update the provider, and then pair it again.

## Next steps [#next-steps]

<Cards>
  <Card href="/docs/plugins/sso/oidc" title="OIDC">
    Register OIDC providers, use discovery, and choose token endpoint authentication.
  </Card>

  <Card href="/docs/plugins/sso/saml" title="SAML">
    Register SAML providers, configure the service provider, and enable IdP-initiated sign-in and Single Logout.
  </Card>

  <Card href="/docs/plugins/sso/saml-security" title="SAML security">
    Review how SAML responses are validated and which algorithms are accepted.
  </Card>

  <Card href="/docs/plugins/sso/provisioning" title="Users and organizations">
    Resolve SSO users, provision accounts, and assign organization membership.
  </Card>

  <Card href="/docs/plugins/sso/domain-verification" title="Domain verification">
    Prove domain ownership with a DNS TXT record.
  </Card>

  <Card href="/docs/plugins/sso/reference" title="Reference">
    Review options, endpoints, errors, and schema.
  </Card>

  <Card href="/docs/guides/saml-sso-with-okta" title="SAML setup guide">
    Set up a SAML connection step by step.
  </Card>
</Cards>

