> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bbrands.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Session and access

> How a customer enters the contract wizard without a password: the one-time link, the contract session, who may call each step, access recovery and the portal password at the end.

A customer never signs in with a password during contracting. They enter with
a one-time link sent by email, the portal exchanges it for a **contract
session**, and the password is created only at the end of the flow.

## Lifecycle

```mermaid theme={null}
sequenceDiagram
    actor C as Customer
    participant P as Portal wizard
    participant B as Contract backend
    participant A as Supabase Auth

    B-->>C: Email with one-time link
    C->>P: Open /{locale}/contract/{id}?action_link=…
    P->>B: POST /contract/{id}/sessions
    B->>A: Verify the one-time token
    A-->>B: User id
    B->>B: Check the user takes part in the contract
    B-->>P: Contract session + ContractState
    P->>B: Wizard calls with the contract session (Bearer)
    Note over P,B: …identity, summary, payment, signature…
    P->>B: PATCH /contract/{id}/password
    B-->>P: ContractState + portal handoff
    P-->>C: Customer portal, already signed in
```

## One-time link

Registration and every access recovery send the same email ("continue your
contract") with a one-time link to
`/{locale}/contract/{id}?action_link=…`. The link is a Supabase Auth magic
link: it can be redeemed once and expires after a while.

## Exchanging the link for a session

`POST /api/v3/contract/{id}/sessions` is public; the one-time token is the
credential. The backend:

1. Loads the contract (`404` when it does not exist).
2. Verifies the token with Supabase Auth.
3. Checks that the user takes part in the contract (see below).
4. Issues an internal token and builds the current `ContractState`.

It answers `{ jwt, state }`. The portal seeds its contract cache from that
response, so the wizard paints the right step with no extra request.

| Answer | Meaning |
| - | - |
| `200` | Session issued |
| `400` | The contract is cancelled, rejected or expired |
| `401` | The token is invalid |
| `403` | The user does not take part in this contract |
| `409` | The link was already used |
| `410` | The link expired |

The internal token carries a unique identifier, so a single session can be
revoked. For a company contract, when account-centric identity is on, the
token names the signer's person account and carries the company as the
organization; a person contract names the person account.

## Who may call each step

Every wizard call sends the contract session as a Bearer token. On the wizard
routes (`/api/v3/contract/{id}/…` for each step) the backend checks, on each
call, that the token is valid, that the contract exists, that the token's
organization (company contracts) or account is the contract's account, and that
the user takes part in it in one of two roles:

| Role | Who | Steps |
| - | - | - |
| `account_member` | Active member of the contract account (the account holder) | Every step, including summary, payment, rewind and cancel |
| `contract_signer` | Active signer of the contract (later company administrators) | Welcome, identity, signer summary, state, PDF preview, signature and password |

When a user is both, `account_member` wins. Steps reserved to account
members answer `403` to a `contract_signer`. The code and signing routes
(`/api/v3/signature/{id}/signer/{signer}/*`) check instead that the session
user owns that signer seat.

The portal stores the session bound to its contract id, so a session from one
contract is never reused on another.

## Re-entry

A customer who comes back through a new link after verifying identity gets a
fresh session. Before that session can request a signing code, the customer
proves the identity card serial again:

* `ContractState` raises `flags.require_signer_document_number`.
* Code, sign and reject requests answer `422 reentry-serial-required` until
  the serial is proved.
* `PATCH /api/v3/contract/{id}/identity` checks the serial with Floid and
  answers with a new session marked as re-verified.

A session that never left the wizard keeps its original token and skips this
check. See [Identity](https://docs.bbrands.io/digital-contracting/identity.md).

## Access recovery

When the session cannot start (`401`, `403`, `410`, a `409` with no stored
session for this contract, an expired stored session, or no link and no stored
session), the portal shows a recovery screen instead of sending the
customer to the Horizon sign-in page. Only an invalid contract id or a server
error takes the customer out of the wizard.

| Endpoint | Who calls it | What it does |
| - | - | - |
| `POST /api/v3/contract/{id}/resend-access` | Public, from the recovery screen | Resends the one-time link to a participant of the contract. Answers the same generic message for any email (`404` when the contract does not exist); rate-limited per contract and email |
| `POST /api/v3/auth/contract/lookup-by-email` | Public; no portal screen calls it today | Takes an email and the account's identity document (RUT or passport); when the email belongs to the account holder and the account has a pending contract, resends the portal link. Every request, a match included, answers the same generic `found: false` with no contract. Rate-limited per IP |
| `POST /api/v3/contract/{id}/resend-portal-magic-link` | Maihue staff (`contract:resend-portal-magic-link`) | Resends the link to the primary signer; rate-limited per contract |

The two resend endpoints answer `409` for a contract in a final status
(`active`, `cancelled`, `rejected`, `expired`). The lookup only searches
contracts that can still continue, so a final contract sends no link.
All three answer `429` when their limit is spent.

## Portal password and handoff

Each signer creates a portal password with
`PATCH /api/v3/contract/{id}/password` once their own signature is done
(before that, the call answers `409 invalid-stage`).

* The response carries the updated `ContractState` and a short-lived handoff
  token (`handoff_bbx`). The portal uses it to open the customer portal
  already signed in. If no handoff can be minted, the portal falls back to the
  normal sign-in.
* A signer who already has a password gets only the handoff: nothing is
  written (a password sent anyway must still meet the rules), and no stage
  check applies.
* A later company administrator gets their own person account here (created
  or reused), and the handoff opens the portal on that account.

## Code

| Concern | Path |
| - | - |
| Session exchange | `app/api/v3/contract/[document_id]/sessions/`, `lib/endpoint/contract-session/` |
| One-time token checks | `lib/common/auth/contract-otp.ts` |
| Participation and session guard | `lib/common/auth/contract-participation.ts`, `lib/common/auth/authorize-contract-session.ts` |
| Session claims | `lib/common/auth/contract-session-claims.ts` |
| Access recovery | `lib/endpoint/contract-resend-access/`, `lib/endpoint/auth/contract-lookup-by-email/`, `lib/endpoint/contract-resend-portal-magic-link/` |
| Password and handoff | `lib/endpoint/contract-password/` |
| Portal recovery screen | `frontend/portal-customer/app/components/digital-contract/contract-access-recovery-state.tsx` |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.