About Auth Profiles
Create, connect, update, and use Auth Profiles for Site authentication.
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 and Astro.locals.siteUser. Its middleware denies access to protected routes when authentication or user resolution fails. Other frameworks can consume the documented gateway contract; 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.
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
- In the Contextual console, open Sites > Authentication.
- Select Create.
- Enter a stable ID, a human-readable Name, and an optional Description.
- For Provider, choose the authentication provider that matches the Site's identity requirements.
- If you choose a bring-your-own provider, enter the settings for the existing Stytch project.
- 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 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 magic links with Managed Stytch. For alternate sign-in methods, configure a BYO project and the application's 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.
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.
- Under Sites > Authentication, open the Auth Profile and its edit form.
- Update its Name, Description, or provider connection settings. You cannot change its ID or Provider.
- Save the changes.
- 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 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.
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 for the complete Astro.locals contract and an example of reading runtime values.
Use an Auth Profile During Local Development
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. This applies only to Managed Stytch; BYO Stytch URL configuration remains governed by its own provider settings.
-
From the Site repository root, copy the example file:
Terminalcp site-auth.local.example.json site-auth.local.json -
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.
-
Select Copy Local Config.
-
In
site-auth.local.json, replace thenullvalue forauthConfigwith the copied object. -
Keep
disableAuthset tofalsewhen testing the sign-in flow. -
Follow CLI installation and tenant sign-in to add or select a configuration for the tenant that contains the Auth Profile. Run
ctxl config loginwith that configuration selected, and confirm the intended tenant withctxl config current --json. -
Set
ctxlConfigIdinsite-auth.local.jsonto that configuration ID. To use the CLI's current configuration instead, leavectxlConfigIdas an empty string. -
Stop any running development server for this Site, then run
yarn astro dev --host localhostso it listens only on your computer. Sign in and open a protected page. The local helper runs the CLI'sfind-or-create-site-useraction 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 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.
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.
- Create or identify a BYO Auth Profile that contains the production Stytch project configuration.
- Create or identify another BYO Auth Profile that contains the non-production Stytch project configuration.
- Follow 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.
- Follow 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.