# About Auth Profiles

These instructions apply to the current Admin Console.

{% hint style="info" %}
Contextual Sites run on Contextual Workers, which are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Workers be enabled for your tenant.
{% endhint %}

Create and connect Auth Profiles, choose a Stytch provider, and prepare authentication configuration for local development.

## How Authentication Works

Site authentication uses three connected resources: an Auth Profile, a Site, and one or more Site Users.

| Resource | Relationship | Result |
| --- | --- | --- |
| **Auth Profile** | Selects the Stytch configuration used for sign-in. | Sites that share an Auth Profile use the same Site User records. |
| **Site** | Optionally references an Auth Profile through its **Auth Profile** field. | The hosted Site Worker's gateway uses that Auth Profile to validate sessions and resolve Site Users. |
| **Site User** | Belongs to one Auth Profile. | The user can sign in to Sites that use the same Auth Profile. |

On a hosted Site, the `managed-site-worker` gateway validates the Stytch session JWT's signature and expiry, then uses its authenticated identity and the Site's Auth Profile ID to resolve the corresponding Site User. The application decides which routes require sign-in and what each user may access, including roles and record-level access.

The Astro starter exposes the gateway's authentication configuration and resolved user as [`Astro.locals.authConfig`](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#understand-local-and-production-runtime-values) and [`Astro.locals.siteUser`](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#use-the-site-user-record). Its middleware denies access to protected routes when authentication or user resolution fails. Other frameworks can consume the [documented gateway contract](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#use-another-framework); `Astro.locals` is the starter's integration, not a requirement for every Site.

The gateway loads Site and authentication configuration at startup and refreshes it about every minute. `SITE_ID=id` follows the latest Site record; `SITE_ID=id#version` pins that Site record. The referenced Auth Profile's own settings still refresh when the Site is pinned. See [Apply Site Configuration Changes](https://docs.contextual.io/build/sites/manage-sites.md#apply-site-configuration-changes).

With BYO Stytch, Contextual can create a Site User with the `member` role on first sign-in. For Consumer, it looks up a user with an email address in the configured Stytch project. For B2B, it looks up a member of the configured organization. Separate Auth Profiles backed by the same Consumer project or B2B organization can admit the same people. To keep sign-in populations separate, use different Consumer projects or B2B organizations.

Authentication is optional. If a Site does not reference an Auth Profile, the standard Site runtime does not require users to sign in.

## Create an Auth Profile

1. In the Contextual console, open **Sites > Authentication**.
2. Select **Create**.
3. Enter a stable **ID**, a human-readable **Name**, and an optional **Description**.
4. For **Provider**, [choose the authentication provider](#choose-an-authentication-provider) that matches the Site's identity requirements.
5. If you choose a bring-your-own provider, enter the settings for the existing Stytch project.
6. Create the Auth Profile.

## Choose an Authentication Provider

When you create an Auth Profile, choose who will manage the Stytch project.

| Provider | Use It When | Required Provider Settings |
| --- | --- | --- |
| **Managed Stytch** | You are creating authentication for a new Site. Contextual provisions and manages the Stytch resources. | No Stytch credentials are required. |
| **BYO Stytch (B2B)** | The solution already uses a Stytch B2B project that it must retain. | Stytch Project ID, secret, public token, organization ID, and organization slug |
| **BYO Stytch (Consumer)** | The solution already uses a Stytch Consumer project that it must retain. | Stytch Project ID, secret, and public token |

Use **Managed Stytch** unless the solution already has a Stytch project or must control its identity-provider configuration. With Managed Stytch, you provide the Auth Profile ID, name, and optional description. Contextual supplies the provider configuration.

Stytch B2B projects organize people as members of an organization. Consumer projects use a user-based model without the organization fields required by the B2B configuration. When bringing an existing project, select the provider type that matches that project. Read [Stytch's authentication overview](https://stytch.com/docs/get-started/guides/authentication) for details about its B2B and Consumer models.

You cannot change the provider after you create the Auth Profile. A bring-your-own Stytch secret remains part of the Auth Profile and is not exposed to Site code or included when you [copy the local configuration](#use-an-auth-profile-during-local-development).

Use magic links with Managed Stytch. For alternate sign-in methods, configure a BYO project and the [application's sign-in methods](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#choose-sign-in-methods).

With Managed Stytch, Contextual registers origins and sign-in redirects for the Worker's default URL and verified custom domains when you create, start, restart, or update the Worker. For BYO Stytch, configure your own project's allowed origins and redirects for the localhost URLs used during development and the hosted Worker URL or custom domain. Custom domains belong to the Worker. See [Authentication and Browser Origins](https://docs.contextual.io/build/sites/site-workers.md#authentication-and-browser-origins).

## Update an Auth Profile

These steps change the settings of an existing Auth Profile. To select a different Auth Profile for a Site, follow [Connect an Auth Profile to a Site](#connect-an-auth-profile-to-a-site).

{% hint style="warning" %}
Changing provider connection settings can disrupt new sign-ins and existing sessions. If a configuration refresh fails, the gateway retains its last successfully loaded configuration. Allow time for refresh and verify the intended behavior after saving.
{% endhint %}

1. Under **Sites > Authentication**, open the Auth Profile and its edit form.
2. Update its **Name**, **Description**, or provider connection settings. You cannot change its **ID** or **Provider**.
3. Save the changes.
4. If you changed provider connection settings, allow time for hosted configuration refresh, then verify sign-in, sign-out, and access to protected routes on each affected Site.

## Connect an Auth Profile to a Site

Follow [Edit a Site](https://docs.contextual.io/build/sites/manage-sites.md#edit-a-site) to select its Auth Profile and save the change. If you enable Managed Stytch on a running Site that previously had no Auth Profile or used BYO Stytch, restart or update its Worker to register its origins. For pinned Site bindings and hosted sign-in, sign-out, and protected-route checks, follow [Apply Site Configuration Changes](https://docs.contextual.io/build/sites/manage-sites.md#apply-site-configuration-changes).

## Use an Auth Profile in a Running Site

In the Astro starter, the `Astro.locals.authConfig` object contains:

| Value | Purpose |
| --- | --- |
| `authProfileId` | Identifies the Contextual Auth Profile used to resolve Site Users. |
| `organizationSlug` | Identifies the Stytch organization for a B2B profile. It is empty for a Consumer profile. |
| `projectId` | Identifies the Stytch project used to validate the session. |
| `publicToken` | Supplies the public Stytch token used by the standard sign-in component. |

The object does not contain the Stytch secret. Read [Site Source Code and Runtime](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#understand-local-and-production-runtime-values) for the complete `Astro.locals` contract and an example of reading runtime values.

## Use an Auth Profile During Local Development

{% hint style="info" %}
This configuration and the `site-auth.local.json` file are only for local development. A hosted Site Worker does not read the file; its gateway loads Auth Profile configuration automatically.
{% endhint %}

The Astro local development server cannot load the Site's hosted Auth Profile automatically. Instead, store the authentication configuration in `site-auth.local.json` and use the Contextual CLI to resolve signed-in Site Users from the tenant. This local helper does not perform the hosted gateway's JWT validation. The starter repository excludes `site-auth.local.json` from Git.

For **Managed Stytch**, Contextual preconfigures a fixed set of common localhost URLs for the Auth Profile, including the Astro default and other common local-development URLs. This is a managed shared allowlist, not a self-service setting: if you need a different localhost port, [contact Contextual support](mailto:help@contextual.io). This applies only to Managed Stytch; BYO Stytch URL configuration remains governed by its own provider settings.

1. From the Site repository root, copy the example file:

   ```bash
   cp site-auth.local.example.json site-auth.local.json
   ```

2. In the Contextual console, open **Sites > Authentication** and select the Auth Profile to use for local development. For BYO Stytch, select the Site's non-production **Test Auth Profile**.
3. Select **Copy Local Config**.
4. In `site-auth.local.json`, replace the `null` value for `authConfig` with the copied object.
5. Keep `disableAuth` set to `false` when testing the sign-in flow.
6. Follow [CLI installation and tenant sign-in](https://docs.contextual.io/build/contextual-workers/quickstart.md#1-check-the-cli-and-connect-to-your-tenant) to add or select a configuration for the tenant that contains the Auth Profile. Run `ctxl config login` with that configuration selected, and confirm the intended tenant with `ctxl config current --json`.
7. Set `ctxlConfigId` in `site-auth.local.json` to that configuration ID. To use the CLI's current configuration instead, leave `ctxlConfigId` as an empty string.
8. Stop any running development server for this Site, then run `yarn astro dev --host localhost` so it listens only on your computer. Sign in and open a protected page. The local helper runs the CLI's `find-or-create-site-user` action automatically.

**Copy Local Config** requests the test configuration held by the selected Auth Profile. For BYO Stytch, follow [Configure BYO Stytch for Production and Local Development](#configure-byo-stytch-for-production-and-local-development) to select the correct profile before copying the configuration.

If you change the Auth Profile used for local development, repeat the copy step and replace `authConfig` in `site-auth.local.json`. Changes in the console do not update the local file.

{% hint style="warning" %}
Never commit `site-auth.local.json` or copy the full configuration into browser code. The CLI stores its authentication separately. Do not copy CLI credentials or access tokens into the Site repository.
{% endhint %}

To temporarily disable authentication during local development, set `disableAuth` to `true` in `site-auth.local.json`. The `authConfig` value can remain `null` until you configure local authentication. With authentication disabled, the local server does not set `Astro.locals.siteUser`. This mode cannot verify sign-in, protected routes, or role-dependent behavior.

## Configure BYO Stytch for Production and Local Development

A BYO Auth Profile stores one Stytch project configuration. Use separate Auth Profiles when the production Site and local development must authenticate against different Stytch projects.

1. Create or identify a BYO Auth Profile that contains the production Stytch project configuration.
2. Create or identify another BYO Auth Profile that contains the non-production Stytch project configuration.
3. Follow [Edit a Site](https://docs.contextual.io/build/sites/manage-sites.md#edit-a-site) to select the production profile under **Auth Profile** and the non-production profile under **Test Auth Profile**, then save. If you changed the primary profile, complete the [Site configuration update and access checks](https://docs.contextual.io/build/sites/manage-sites.md#apply-site-configuration-changes).
4. Follow [Use an Auth Profile During Local Development](#use-an-auth-profile-during-local-development) with the non-production profile selected in the console. Use a Contextual CLI configuration for the tenant that contains that profile.

The hosted Site Worker uses the Site's **Auth Profile**. It does not use **Test Auth Profile** or `site-auth.local.json`. The **Test Auth Profile** field records the non-production profile intended for local development; selecting it does not copy or apply configuration to the local repository.

For BYO Stytch, **Copy Local Config** returns the project values stored on the Auth Profile that you opened. It does not convert a production BYO profile into a non-production configuration.
