> You are reading Better Auth documentation for `v1.8 (Beta)`. This is not the current stable release. APIs may differ from the latest stable version.

# SSO users and organizations (/docs/beta/plugins/sso/provisioning)

Control how SSO identities map to users, run provisioning code, and add users to organizations.



Every SSO sign-in ends with a Better Auth user.

## How SSO users are matched [#how-sso-users-are-matched]

Better Auth stores each SSO identity as an `account` keyed by the provider ID and the OIDC `sub` claim or the SAML `NameID`. Only that pair identifies the person, not the email ([OIDC Core §5.7](https://openid.net/specs/openid-connect-core-1_0.html#ClaimStability), [SAML Core §2.2.3](https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf)). A provider can assert any email address. Better Auth links by email only when the checks below pass.

* **Returning account.** The user who owns the account signs in.
* **New account, no user with that email.** A new user is created with `emailVerified: false`. To refuse users you have not provisioned, use [`resolveUser`](#resolve-sso-users) with `{ action: "reject" }`.
* **New account, a user with that email exists.** Better Auth links the account in these cases:

  * [`resolveUser`](#resolve-sso-users) returns `link`. The account is linked to the `userId` it names.
  * All of these hold. The account is linked to the user with that email.
    * The provider's [domain is verified](/docs/beta/plugins/sso/domain-verification#enable-domain-verification). A [`defaultSSO`](/docs/beta/plugins/sso#configure-providers-in-code) provider counts as verified only when `domainVerification.enabled` is `true`.
    * The email's domain is one of the provider's domains or a subdomain of one. For example, `user@eng.acme.com` matches a provider for `acme.com`.
    * The existing user's email is verified. `account.accountLinking.requireLocalEmailVerified: false` skips this check. Keep the default: without it, someone who registers an account for another person's email before they do can have that person's later SSO sign-in linked into the account they control.
    * Account linking is on: `account.accountLinking.enabled` is not `false`, and `account.accountLinking.disableImplicitLinking` is not `true`.
  * The deprecated `trustEmailVerified` option is on, and the IdP reports the email verified. The account is linked to the user with that email without domain verification. The existing user's email must still be verified, and account linking must be on.

  Otherwise the sign-in fails. OIDC redirects with `error=account not linked`, and SAML with `error=account_not_linked`. To let the sign-in through, meet one of the cases above. For example, verify the provider's domain when the other conditions in that case already hold, or return `link` from `resolveUser`.

`account.accountLinking.trustedProviders` does not apply to SSO providers, and registration rejects a `providerId` listed there. Whoever registers an SSO provider chooses its ID. For this reason, trust comes only from a verified domain.

<Callout type="warn">
  The deprecated `trustEmailVerified` option trusts the IdP's `email_verified` claim or mapped attribute. Anyone who can register a provider can then claim any email address and take over its account. Use domain verification or `resolveUser` instead.
</Callout>

### Block new users [#block-new-users]

To turn off implicit sign-up for SSO, set `disableImplicitSignUp: true`. A sign-in that would create a user then fails with `error=signup disabled` (SAML: `error=signup_disabled`). To let such a user in, create the user yourself and return `link` from [`resolveUser`](#resolve-sso-users).

SAML ignores `requestSignUp`, but an OIDC sign-in still creates the user when `signIn.sso` is called with `requestSignUp: true`. Any client can send that flag. For OIDC, `disableImplicitSignUp` only changes the default. To refuse users you have not provisioned, return `{ action: "reject" }` from `resolveUser`.

```ts title="auth.ts"
sso({
  disableImplicitSignUp: true, // [!code highlight]
});
```

### Update profiles on sign-in [#update-profiles-on-sign-in]

Better Auth sets the user's profile only when it creates the user. To refresh the profile on every sign-in, set `overrideUserInfo: true` on an OIDC provider. Each sign-in then overwrites the user's `name`, `email`, `image`, and mapped additional fields with the IdP's values, after `mapProfileToUser` when it is set. Enable it only for IdPs that control their users' email addresses.

* A changed email is stored unverified, unless the deprecated `trustEmailVerified` option is on and the IdP reports that same address verified. An email from `mapProfileToUser` that differs from the IdP's is always unverified.
* Set it per provider in the registration body. To apply it to every provider registered afterwards, set `defaultOverrideUserInfo: true` on the plugin.
* SAML sign-ins update the profile only when `resolveUser` returns `profile: "update"`.

## Providers without an email [#providers-without-an-email]

Better Auth needs an email to find, link, and create the user. It reads the email from the provider's `mapping`. For OIDC, the default is the `email` claim. For SAML, Better Auth takes the first of these that is present:

1. The `samlConfig.mapping.email` attribute, which defaults to `email`.
2. The `NameID`, only when its `Format` is `urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress`.
3. When `samlConfig.mapping.email` is not set, the `http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress`, `mail`, or `urn:oid:0.9.2342.19200300.100.1.3` attribute, in that order.

A `NameID` with any other `Format`, or with no `Format`, is not used as the email, even when its value looks like an email address. Persistent and transient NameIDs are opaque, and an unspecified NameID is often a username that users can edit. To pin the email to one attribute your IdP administrator controls, set `samlConfig.mapping.email`.

When no email is found, sign-in fails. OIDC redirects with `error=invalid_provider&error_description=missing_user_info`, and SAML with `error=missing_user_info`. For SAML, the server log names the `NameID` Format the IdP sent and the attributes it included. Many IdPs send the email as an `unspecified` NameID with no attributes by default. To fix that, configure the IdP to send an `email` attribute or the `emailAddress` Format, or set `samlConfig.mapping.email` to the attribute it does send.

To sign in users from a provider that sends no email, return one from `mapProfileToUser`. It runs on every OIDC and SAML sign-in, after `mapping` is applied and before the user is looked up:

```ts title="auth.ts"
import { betterAuth } from "better-auth";
import { createPlaceholderEmail } from "@better-auth/core/utils/email";
import { sso } from "@better-auth/sso";

export const auth = betterAuth({
    plugins: [
        sso({
            mapProfileToUser: ({ providerId, accountKey, providerUser }) => ({
                email:
                    providerUser.email ??
                    createPlaceholderEmail({
                        identifier: accountKey.accountId,
                        namespace: providerId,
                    }),
            }),
        }),
    ],
});
```

<Callout type="warn">
  The returned email decides which existing user a new account links to, as [How SSO users are matched](#how-sso-users-are-matched) describes. An unverified email still links to an existing user when the email is in one of the provider's verified domains. Return only addresses the provider vouches for, or a placeholder from `createPlaceholderEmail`. A placeholder is on the reserved `.invalid` domain, which never matches a verified domain.
</Callout>

The callback receives `protocol`, `providerId`, `accountKey`, and `providerUser`, plus `providerClaims` and `verifiedIdTokenClaims` for OIDC or `providerAttributes`, `nameID`, and `nameIDFormat` for SAML. `providerUser.emailVerified` is `false` unless the deprecated `trustEmailVerified` option is on.

It can return `email`, `name`, and `image`. An email that differs from the provider's must be an email address. A field you omit keeps the provider's value. Other keys are ignored. To set additional user fields, use `mapping.extraFields` or `provisionUser`. This sign-in never marks a returned email verified when it differs from the provider's. The callback cannot return `emailVerified`. The stored email is verified only when the provider asserts it with the deprecated `trustEmailVerified` option on. Domain verification lets an unverified email link to an existing user; it does not mark the email verified. The account identity always stays the provider's `sub` claim or `NameID`.

To refuse the email the provider sent, return `email: null`. Sign-in then fails with `missing_user_info`.

The mapped values replace the provider's in `providerUser` for `resolveUser`, in `userInfo` for `provisionUser`, and in `userInfo` for an OIDC `getRole`. The provider's own values stay in `providerClaims`, `verifiedIdTokenClaims`, or `providerAttributes`.

Sign-in redirects with `error=SSO_PROFILE_MAPPING_FAILED` when the callback does any of these:

* Throws.
* Returns a value that is not an object, such as `undefined` or an array.
* Sets `email` to something other than a string, `null`, or `undefined`, or to a string that differs from the provider's email and is not an email address.
* Sets `name` or `image` to something other than a string or `undefined`.

`createPlaceholderEmail` builds `<identifier>@<namespace>.placeholder.invalid` and throws when the result is not a valid email address. Sign-in then fails with `error=SSO_PROFILE_MAPPING_FAILED`. The example works when both of these hold:

* The provider ID contains only letters, digits, and hyphens, and starts with a letter or digit.
* The subject contains only letters, digits, `_`, `+`, and `-`.

Many SAML persistent `NameID` values are Base64 and contain `/` or `=`. Hex-encode a value that may contain other characters. Emails are compared case-insensitively, so also hex-encode subjects that can differ only by letter case. Placeholder emails cannot receive mail. See [Handling Providers Without Email](/docs/beta/concepts/oauth#handling-providers-without-email).

The returned email replaces the stored one when `resolveUser` returns `link` with `profile: "update"`. Without that decision, it replaces the stored one on every sign-in through an OIDC provider with [`overrideUserInfo`](#update-profiles-on-sign-in). If the provider stops sending the email, a placeholder then overwrites the user's real address.

### Use an email-valued NameID [#use-an-email-valued-nameid]

When you cannot change an IdP's settings, you can use its email-valued `unspecified` `NameID` as the email. Do this only for providers whose IdP sets the `NameID` from an email its administrator controls. If users can edit the value, such as a username, a user could set it to another person's address. List those providers by ID:

```ts title="auth.ts"
// Providers whose NameID is an admin-controlled email.
const NAME_ID_EMAIL_PROVIDERS = new Set(["acme"]);
const UNSPECIFIED_NAME_ID_FORMATS = [
    undefined,
    "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
    "urn:oasis:names:tc:SAML:1.0:nameid-format:unspecified",
];

sso({
    mapProfileToUser: (input) =>
        input.protocol === "saml" &&
        NAME_ID_EMAIL_PROVIDERS.has(input.providerId) &&
        !input.providerUser.email &&
        UNSPECIFIED_NAME_ID_FORMATS.includes(input.nameIDFormat) &&
        input.nameID.includes("@")
            ? { email: input.nameID }
            : {},
});
```

The email from the `NameID` is stored unverified. A `NameID` without `@` is skipped, so the sign-in fails with `error=missing_user_info`. A value with `@` that is not an email address fails with `error=SSO_PROFILE_MAPPING_FAILED`.

## Resolve SSO users [#resolve-sso-users]

To map an OIDC or SAML identity to an existing Better Auth user instead of relying on email matching, use `resolveUser`. The resolver runs on every SSO sign-in, after Better Auth has verified the protocol identity.

```ts title="auth.ts"
import { betterAuth } from "better-auth";
import { sso } from "@better-auth/sso";
import {
    findProvisionedUserByAccountKey,
    validateOIDCClaims,
    validateSAMLAttributes,
} from "@/lib/provisioning";

const auth = betterAuth({
    plugins: [
        sso({
            resolveUser: async (input, context) => {
                if (input.providerId !== "workforce") {
                    return { action: "continue" };
                }

                if (input.protocol === "oidc") {
                    await validateOIDCClaims(input.verifiedIdTokenClaims);
                } else {
                    await validateSAMLAttributes(input.providerAttributes);
                }

                const provisionedUser = await findProvisionedUserByAccountKey({
                    providerId: input.providerId,
                    issuer: input.accountKey.issuer,
                    subject: input.accountKey.accountId,
                    database: context.database,
                });

                if (!provisionedUser) {
                    return {
                        action: "reject",
                        code: "PROVISIONED_USER_NOT_FOUND",
                    };
                }

                return {
                    action: "link",
                    userId: provisionedUser.userId,
                    profile: "preserve",
                };
            },
        }),
    ],
});
```

Your resolver receives `input` and `context`. `input.protocol` is `"oidc"` or `"saml"`, and `input.providerId` is the provider's ID. `input` also holds:

* `accountKey.issuer`: the ID token's `iss` for OIDC, or the IdP entity ID for SAML.
* `accountKey.accountId`: the ID token's `sub` for OIDC, or the signed `NameID` for SAML. `NameID` matching is case-sensitive.
* `providerUser`: the normalized profile, after [`mapProfileToUser`](#providers-without-an-email) when it is set.
* `verifiedIdTokenClaims` (OIDC only): the full verified ID token payload.
* `providerClaims` (OIDC only): the UserInfo claims, or the ID token claims when there is no UserInfo endpoint.
* `providerAttributes` (SAML only): the verified assertion attributes. Multi-valued attributes stay arrays.
* `providerReference`: opaque and transient. Never persist it as a tenant or user binding.

Profile fields, claims, and attributes may still need validation before you use them for authorization. `findProvisionedUserByAccountKey` is your own function in this example. It should resolve the exact provider, issuer, and subject tuple through the transaction-scoped `context.database` adapter. Do not fall back to matching an email address when the exact provisioned subject is absent.

Your resolver returns one of three decisions:

* `{ action: "continue" }` continues the normal SSO sign-in behavior.
* `{ action: "link", userId, profile }` links the exact SSO account to that user. Use `profile: "preserve"` to keep the local profile or `profile: "update"` to update it from the provider profile.
* `{ action: "reject", code, message? }` rejects the sign-in with an application-defined error. No account or session is created.

After a rejection, the browser goes to the first of these that is set:

1. The `errorCallbackURL` passed to `signIn.sso()`.
2. `onAPIError.errorURL`.
3. `{baseURL}/error`.

IdP-initiated SAML sign-ins go to the [sign-in destination](/docs/beta/plugins/sso/saml#acs-and-callback-urls) instead. The redirect carries `error` set to `code`, and `error_description` set to `message` when you give one. Read them on the target page to show the user why the sign-in failed.

If the resolver throws or returns an invalid decision, the sign-in fails with `error=SSO_USER_RESOLUTION_FAILED`. The server log says which of the two happened.

<Callout type="warn">
  `resolveUser` requires:

  * a database adapter with native transactions and async-context support
  * an ID token in every OIDC sign-in
  * with secondary storage, `session.storeSessionInDatabase: true`, and `session.preserveSessionInDatabase` left off
</Callout>

A fresh sign-in attempt may call the resolver again, so keep it idempotent. Writes made through `context.database` during resolution, linking, and session creation commit or roll back together, and session cookies are set only after the commit. Network calls, other external side effects, and after-commit hooks are not rolled back.

## User provisioning [#user-provisioning]

To run your own code when an SSO sign-in creates a user, set `provisionUser`. It receives `{ user, userInfo, token, provider }` and returns nothing. To also run it on every later SSO sign-in, set `provisionUserOnEveryLogin: true` next to `provisionUser`. It runs before [organization provisioning](#organization-provisioning).

`userInfo` holds the mapped profile, including any `mapping.extraFields`. `token` holds the OIDC tokens and is `undefined` for SAML.

```ts title="auth.ts"
sso({
  provisionUser: async ({ user, userInfo }) => {
    await upsertProfile(user.id, {
      department: userInfo.department, // from mapping.extraFields
    });
  },
});
```

If `provisionUser` throws, the sign-in response fails but the user stays created. Without `provisionUserOnEveryLogin`, the callback does not run again for that user. For this reason, catch errors from calls the sign-in does not depend on. With `provisionUserOnEveryLogin`, make the callback idempotent.

## Link a provider to an organization [#link-a-provider-to-an-organization]

To add users who sign in through a provider to an organization, pass `organizationId` when you register the provider. When you call this from a route a browser reaches with its session cookie, [check its `Origin`](/docs/beta/reference/security#csrf-protection) first.

```ts title="register-provider.ts"
await auth.api.registerSSOProvider({
  body: {
    providerId: "acme",
    issuer: "https://idp.acme.com",
    domain: "acme.com",
    organizationId: "org_acme", // [!code highlight]
    samlConfig: { /* ... */ },
  },
  headers: request.headers,
});
```

The caller must be a member of the organization, or the call returns `400`. With the organization plugin, the caller must also be an organization `owner` or `admin`, or it returns `403`. Owners and admins can then list, update, and delete the provider. Without the plugin, only membership is checked, and only the user who registered the provider can manage it.

## Organization provisioning [#organization-provisioning]

Organization provisioning adds SSO users to organizations as members. You need the [organization plugin](/docs/beta/plugins/organization) for it.

* **Sign-in through a provider with an `organizationId`.** The user joins that organization with `defaultRole` (`"member"` by default). When you set `getRole({ user, userInfo, token, provider })`, the user gets the role it returns instead: `"member"` or `"admin"`.
* **Sign-in through a social provider.** The user joins an organization only when all of these hold:
  * Domain verification is on.
  * The stored user's email is verified.
  * The email's domain matches the verified providers of exactly one organization.

Nothing happens when the user is already a member or has a pending, unexpired invitation to that organization. Automatic provisioning neither accepts nor cancels the invitation. To turn provisioning off, set `organizationProvisioning.disabled: true`.

In `getRole`, `userInfo` is the mapped profile for OIDC, the raw assertion attributes for SAML, and empty for assignment by domain. For OIDC, a claim such as `groups` is present only when you map it through `mapping.extraFields`.

An organization `admin` can list, update, and delete that organization's SSO providers. Returning `"admin"` from an IdP group claim therefore hands control of the organization's IdP trust to whoever manages that group at the IdP.

```ts title="auth.ts"
sso({
  organizationProvisioning: {
    defaultRole: "member",
    getRole: async ({ userInfo }) => {
      const groups = [userInfo.groups].flat();
      return groups.includes("sso-admins") ? "admin" : "member";
    },
  },
});
```

