> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Third-party login (deprecated)

Describes integration with third-party authentication systems

**This is a paid feature**
This feature is available on certain Enterprise plans. [Talk to sales](https://www.sanity.io/contact/sales?ref=docs) to learn more.

> [!NOTE]
> Looking for SAML SSO?
> This article discusses the **deprecated, legacy method** of implementing third-party login. Use [SAML single sign-on (SSO)](https://www.sanity.io/docs/developer-guides/sso-saml) instead.

If custom access control is activated on your plan, you can replace a project's user database with your own login solution. For example, you can integrate with an SSO system such as Active Directory or Kerberos.

Sanity provides APIs to register users and permissions for datasets and to create Sanity sessions. You're responsible for implementing the integration with your authentication system.

Implementing third-party login involves:

- Registering Sanity groups with appropriate permissions through the API, either manually or with code.
- Writing code to generate a Sanity session when a user logs in.
- Optionally, modifying Sanity Studio to use a separate login form.

The following sections describe each of these steps.

We also have a [sample Node.js application](https://github.com/sanity-io/3rd-party-auth-example) that shows how to do this using Passport.js. It supports authenticating with Google OAuth and Okta SAML.

## Manage groups and users

Sanity uses groups to grant permissions to users. First, set up the groups and permissions you need, as well as group memberships for users. Depending on your use case, setting up the groups manually may be enough, but we expect most projects to need code that keeps groups in sync with their local user database.

> [!WARNING]
> Gotcha
> Custom user IDs must adhere to the following rules:
> - Must begin with a lowercase `e`
> - Followed by a string of any combination of uppercase or lowercase characters from the English alphabet, numbers, hyphens, and underscores. For example, `e-A3f-Lm_N6-eQrS-Xw9y`
> This can also be expressed with the following regular expression: `/^e[a-zA-Z0-9-_]+$/`. User IDs can be at most 300 characters long.

> [!TIP]
> Protip
> You don't need to create Sanity users that correspond to your local users. Listing the user IDs as group members is enough.

> [!WARNING]
> Gotcha
> You cannot assign a third-party login user to a role created with the [roles API](https://www.sanity.io/docs/user-guides/roles). We recommend using [SAML integration](https://www.sanity.io/docs/developer-guides/sso-saml) instead where possible. You can create roles for third-party login users with groups.

Groups are stored as regular Sanity documents of type `system.group` under the `_.groups.` path. Use regular [mutations](https://www.sanity.io/docs/http-reference/mutation) through the API to create and modify them.

For example, to give the journalists `e-henrik` and `e-emma` in your Norway office full access to all articles in the `norway` edition, but only read access to other editions, create the following group:

```json
{
  "_id": "_.groups.office-norway",
  "_type": "system.group",
  "grants": [
    {
      "filter": "_type == 'article' && edition._ref == 'norway'",
      "permissions": ["create", "update", "read"]
    },
    {
      "filter": "_type == 'article'",
      "permissions": ["read"]
    }
  ],
  "members": ["e-henrik", "e-emma"]
}
```

> [!TIP]
> Protip
> Sanity user IDs may be exposed in publicly available data (for example, as the author of a document), so take care not to use any personally identifiable information when generating IDs. An arbitrary number or a hash is usually a good choice.

After you set up your groups and memberships, give users a Sanity token when they log in.

## Generate Sanity tokens

You need a login solution on your end that authenticates users with your user database and then makes an API call to Sanity to generate a session claim. The details depend on the authentication system you use, but Sanity has a GitHub repository with a [complete example](https://github.com/sanity-io/3rd-party-auth-example).

After you authenticate the user, make an HTTP `POST` request using a [robot token](https://www.sanity.io/docs/content-lake/http-auth) that has the `create-session` role to the following endpoint:

`POST https://YOUR_PROJECT_ID.api.sanity.io/v2021-06-07/auth/thirdParty/session`

The `POST` body can be JSON or URL-encoded, and contains the following fields:

- `userId`: the user's ID, as listed in the group's members.
- `userFullName`: the user's full name.
- `userEmail`: optional email address for the user.
- `userImage`: optional HTTPS URL to the user's profile image.
- `userRole`: optional role for the user. If the user needs to log in to Sanity Studio, the role must be either `administrator` or `editor`.
- `sessionExpires`: ISO 8601 timestamp for when the session expires. Must be in the future.
- `sessionLabel`: optional label for the session.

The API call returns JSON that includes `token` and `endUserClaimUrl`. If the session is for managing content in Sanity Studio, the `endUserClaimUrl` contains a URL that the end user's browser can visit to obtain a Sanity session (set as a cookie). This URL is valid for a single use. You can add an `origin` query parameter with a URL to redirect the user to after the session is created. This URL must be listed as an allowed [CORS origin](https://www.sanity.io/docs/content-lake/cors) for the project. For other use cases, such as native applications, store the `token` in a secure location and use it to authenticate requests to the Sanity API.

### User profiles

Every time you create a third-party session, the user information you post with it is saved to a user profile. The profile is attached to the user ID and project ID, and is only available to users logged in to your project. Sanity keeps the profile so it can display user information after the session ends, when the user logs out or the session expires. You're responsible for deleting these profiles when they should be deleted, according to your terms. To delete a profile, send a DELETE request:

```text
DELETE https://api.sanity.io/v2021-06-07/projects/YOUR_PROJECT_ID/users/THIRD_PARTY_USER_ID/profile
Authorization: Bearer CREATE_SESSION_TOKEN

```

To create or update a user profile explicitly, send a PUT request with the details:

```text
PUT https://api.sanity.io/v2021-06-07/projects/YOUR_PROJECT_ID/users/THIRD_PARTY_USER_ID/profile
Content-Type: application/json
Authorization: Bearer CREATE_SESSION_TOKEN

{
  "name": "Some username",
  "profileImage": "https://optional.user.img/url.jpg"
}

```

## Use external logins in Sanity Studio

To use your own login solution instead of the default login providers, set the `auth` option in your studio's `sanity.config.ts` file. A static `providers` array replaces the default providers:

**sanity.config.ts**

```typescript
import {defineConfig} from 'sanity'

export default defineConfig({
  // ...projectId, dataset, and the rest of your configuration
  auth: {
    redirectOnSingle: true,
    providers: [
      {
        name: 'custom-login',
        title: 'Custom Login',
        url: 'https://mydomain.com/login',
        logo: '/static/custom-login.png',
      },
    ],
  },
})
```

The studio redirects the user to the specified `url` to start authentication. The `title` and `logo` appear on the studio login screen. To support several authentication solutions, add more than one provider to the array.

