# About Site Users

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 update Site Users, access the authenticated user in Site code, and use Site User roles in application authorization.

## How Site Users Work

Site authentication connects an Auth Profile, a Site, and one or more Site Users. Each Site User belongs to one Auth Profile and can sign in to Sites that reference that profile.

On first sign-in with BYO Stytch, Contextual can create a Site User with the `member` role for an existing provider identity. The person must sign in as a Consumer user with an email address in the configured project or as a member of the configured B2B organization. If two profiles use the same provider population, the same person can sign in through both and have a separate Site User record in each.

Deploying an application image does not create Site Users. To [verify a deployed Site](https://docs.contextual.io/build/sites/releases.md#verify-the-deployment), use an account for the target tenant's Auth Profile. You can create its Site User in advance to choose the role.

The hosted Site Worker's gateway validates the Stytch session JWT and resolves the corresponding Site User. The Astro starter makes the record available to server-side Site code as `Astro.locals.siteUser`. The record supplies the user's identity and `admin` or `member` role. Site application code must use those values to enforce any role-based access rules.

Create and manage Site Users under **Sites > Site Users** in the Contextual console.

## Create and Connect a Site User

A Site User record contains an email address, an optional name, an Auth Profile, and an `admin` or `member` role.

To create and connect a Site User to a Site:

1. Under **Sites > Site Users**, select **Create**.
2. Enter the user's email address and optional name.
3. Select the same Auth Profile used by the Site.
4. Select the user's `admin` or `member` role and save the Site User.

The Site User can sign in to Sites that reference the selected Auth Profile.

### Permissions to Create a Site User

1. In the Admin Console, open **Admin → Roles** to grant access to a person, or **Admin → API Keys** to grant access to an application.
2. Select the Role or API Key and open **Permissions**.
3. Under **Service components**, select the permissions for the operation, then click **Save permissions**.

| Operation | Site Users | Site Authentication |
| --- | --- | --- |
| Create a Site User | **Read**, **Create** | **Read** |
| Delete a Site User | **Read**, **Delete** | **Read** |

Both operations read the user's Auth Profile. If the profile uses your own Stytch project, also grant **Reveal** under **Site Authentication** so the platform can use the profile's Stytch secret.

These permissions do not require access to **All Object Types**.

## Update a Site User

1. Under **Sites > Site Users**, open the Site User and its edit form.
2. Update the user's **Name** or **Role**. The **ID**, **Email**, and **Auth Profile** fields are read-only.
3. Save the changes.

Hosted Site Users are cached by session JWT for up to three minutes, so saved role or name changes can take that long to appear. A different JWT uses a separate cache entry. The Astro local helper caches Site Users for one minute. Updating a Site User does not require a new application release.

The gateway checks session JWT signatures and expiry. Server-side session revocation alone does not immediately invalidate an already issued JWT; the gateway can accept it until it expires. Signing out through Stytch's SDK clears the current browser's session cookie.

Changing a Site User's role has no effect on what the user can access unless the Site's application code enforces the authorization behavior associated with that role. To learn more, see [Understand Site User Roles and Authorization](#understand-site-user-roles-and-authorization).

## Use the Authenticated Site User in Site Code

In the Astro starter, `Astro.locals.siteUser` contains the resolved Site User. It is `undefined` when signed out, when the session is invalid, or when there is no Auth Profile. A value of `null` means a valid session's Site User could not be resolved; treat it as an error. The hosted gateway can retry failed user resolution on a subsequent request after five seconds.

The starter's middleware allows a resolved user to open protected routes. Other frameworks can consume the [documented gateway contract](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#use-another-framework) and must enforce their own route and operation access rules; `Astro.locals` is the starter's integration.

Astro API endpoint handlers receive the same Site User through their `locals.siteUser` context and must check the user's access before performing an operation.

To understand how the Astro starter handles local authentication, public paths, and signed-out redirects, see [Distinguish Server Output and Middleware](https://docs.contextual.io/build/sites/site-source-code-and-runtime.md#distinguish-server-output-and-middleware).

## Understand Site User Roles and Authorization

Sites provides a standard way to authenticate users. The current Sites runtime does not provide a managed authorization model for deciding what an authenticated user may do.

The Site User record includes an `admin` or `member` role. Site code can inspect that role, but the standard Sites runtime does not use it to decide which pages, actions, or data the user may access. The role is an input that a Site can use for application-specific authorization; it is not an enforced permission.

Sites does not automatically map a Site User role to Contextual API permissions or provide per-user Tenant API credentials. The default Site application receives no platform SDK or tenant credentials. A separate Worker's [platform identity](https://docs.contextual.io/build/contextual-workers/managed-workers/platform-services.md#worker-identity) does not establish what a Site User may access. Any backend with platform access must authorize the Site User's access to the requested operation and records.

{% hint style="warning" %}
Showing or hiding part of a Site based on the Site User's role does not, by itself, restrict the corresponding backend operation. The server endpoint must use the verified Site User and the application's access rules to authorize the operation on the requested records. Do not trust a user ID, email address, or role supplied by the caller as proof of identity or permission.
{% endhint %}
