> ## 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.

# Authentication

> How authentication works across the MaihueGO app, horizon-authenticate, portal-customer and horizon-api, and where each piece of the documentation lives.

> [!NOTE]
>
> This section explains, step by step, how a person creates their access, signs in and keeps their
> session in MaihueGO (iOS and Android) and in the web apps, and how all of it is operated in each
> environment. It is written for release **2026.11.3**. The internal documentation, with
> requirements, decisions and status, lives in
> [`docs/initiatives/authenticate/`](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/initiatives/authenticate/README.md)
> and the stable definition of the domain in
> [`docs/definitions/authentication.md`](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/definitions/authentication.md).

## What the domain covers

The `authenticate` domain (ADR 0131) groups everything related to getting into the platform:
registration by e-mail or with a provider, sign-in with a password, a provider or a magic link,
optional passwords, sessions and SSO between apps, linking logins to an account, the holding's
identity provider (IdP), machine authentication, auth events and security hardening. The identity
engine today is **Supabase Auth (GoTrue)**; horizon-api wraps it and issues its own internal JWT
(Horizon JWT).

![The MaihueGO app, horizon-authenticate, portal-customer and the other web apps call the auth endpoints and the identity provider of horizon-api. horizon-api reads flags and rate limits in Redis, signs in through Supabase Auth with Google, Apple and Microsoft, stores provider data in Postgres, talks to Apple ID for tokens, revocation and notifications, and sends auth e-mails through the notification core and Resend, behind Apple's Private Email Relay for hidden e-mails.](https://mintcdn.com/bbrands/-6vpv4Xb-mMAz8bx/images/authenticate/auth-architecture.svg?fit=max\&auto=format\&n=-6vpv4Xb-mMAz8bx\&q=85\&s=da344e59efdfc90c65011d0c48149c10)

How authentication fits together

## Key words

These words have a precise meaning across the whole domain. Use them this way in code, tickets and
conversations with support.

| Word | What it is |
| - | - |
| **Login** | The credential someone gets in with: one Supabase Auth user with its e-mail, its identities and its `user_metadata` row. It gives access to accounts, but owns no identity, roles or usage (ADR 0115). |
| **Account** | The person or company that owns identity, roles, usage and subscriptions. A login reaches it through `account_user`. |
| **Identity** | A way the provider knows the login (`auth.identities`: `email`, `google`, `apple`, `azure`). One login may have several. |
| **Provider** | `google`, `apple` or `microsoft` (Supabase calls it `azure`). Each one has its own feature flag (ADR 0133). |
| **Method** | How the person proves who they are this time: password, provider or magic link. |
| **Password** | Optional for every login (ADR 0132). `has_created_password` says whether it exists. Rule: 4 of 5 criteria plus confirmation. |
| **Magic link** | Single-use sign-in link, valid for one hour, for logins without a password. A client POST consumes it (ADR 0095, ADR 0134). |
| **Session** | GoTrue tokens on the device plus the Horizon JWT for the API; on the web, a per-environment SSO cookie (ADR 0014). |
| **Pending login** | A login with no active `account_user`: a provider sign-in that did not finish registration or that stopped at a document conflict. It can finish registration or be linked (ADR 0136). |
| **Private relay e-mail** | `@privaterelay.appleid.com` address from Apple's "Hide My Email". It only accepts mail from domains registered with Apple; `user_metadata.is_private_email` marks it. |

> [!TIP]
>
> The most common confusion is "login" versus "account". One person can have **one login with several
> identities** (e-mail and Apple, for example) and that login can give access to **several accounts**.

## The clients and what each one does

| Client | Where | What it does in authentication |
| - | - | - |
| MaihueGO app | iOS and Android (Expo) | Registration and sign-in by e-mail, Google, Apple (native on iOS, browser on Android) and Microsoft; magic link through a universal link; alert to create a password on Home; password change in Settings; "Es mi cuenta, vincular" linking. |
| horizon-authenticate | `auth.bbrands.io` (`development.auth.bbrands.io`, `certification.auth.bbrands.io`) | The one place to sign in on the web: e-mail first, password, providers, magic link, registration, recovery, verification, OAuth welcome step and the IdP consent page. Hands the session to the target app (`next`). |
| portal-customer | `portal.maihuego.com` | Receives the session through SSO, shows the "Crea tu contraseña" banner and the Mi cuenta › Seguridad section. Its host serves the universal links that open the app with the magic link. |
| horizon-api | `api.bbrands.io` (`development.api.bbrands.io`, `certification.api.bbrands.io`) | Every `/api/v3/auth/*`, `/api/v3/oauth/*` and `/api/v3/idp/*` endpoint; talks to Supabase Auth and Apple; evaluates the flags; applies rate limits; issues the Horizon JWT. |
| Other web apps | Horizon Enterprise and others | Sign in through horizon-authenticate and SSO; they have no sign-in screens of their own. |

## The pages of this section

> [card] [E-mail-first sign-in](https://docs.bbrands.io/maihuego/authentication/sign-in.md)
>
> E-mail first on every surface: `verify-email`, the route table and password sign-in.

> [card] [Registration](https://docs.bbrands.io/maihuego/authentication/registration.md)
>
> E-mail and provider registration, verification, required data and account migration.

> [card] [Google, Apple and Microsoft](https://docs.bbrands.io/maihuego/authentication/oauth.md)
>
> Per-platform behaviour, per-provider flags, contracts and limits.

> [card] [Apple: tokens and notifications](https://docs.bbrands.io/maihuego/authentication/apple.md)
>
> Client secret, encrypted refresh token, revocation on account deletion and Apple notifications.

> [card] [Magic link](https://docs.bbrands.io/maihuego/authentication/magic-link.md)
>
> The sign-in link for logins without a password and the portal activation link.

> [card] [Password](https://docs.bbrands.io/maihuego/authentication/password.md)
>
> Optional password: create, change, recover and the 4-of-5 rule.

> [card] [Identity linking](https://docs.bbrands.io/maihuego/authentication/identity-linking.md)
>
> A pending login that links to the account owning its document.

> [card] [Session](https://docs.bbrands.io/maihuego/authentication/session.md)
>
> Horizon JWT, sign-out, per-environment cookie and SSO handoff.

> [card] [Platform](https://docs.bbrands.io/maihuego/authentication/platform.md)
>
> The holding's IdP, machine authentication, auth events and the sign-in shell.

> [card] [Security](https://docs.bbrands.io/maihuego/authentication/security.md)
>
> Rate limits, lockout, environment switches and accepted risks.

> [card] [Rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)
>
> What to configure in each environment, in order, up to the mandatory release.

> [card] [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md)
>
> Rotations, rollback and day-to-day diagnosis.

## What changes in 2026.11.3

Everything ships in a single PR (#1960, label `large-change`):

1. **Sign in with Apple comes back**, after the App Review rejection under guideline 4.8: native on
   iOS and through the browser with a Services ID on Android and the web (ADR 0135).
2. **Provider registration asks for no password** (ADR 0132). The password is optional and offered
   later: Home alert and Settings in the app, banner and Mi cuenta › Seguridad in the portal.
3. **One flag per provider for every platform**: `authenticate-oauth-google`,
   `authenticate-oauth-apple` and `authenticate-oauth-microsoft`, category `authenticate`, off by
   default (ADR 0133, supersedes ADR 0048). The old `maihuego-application-oauth-*` keys are deleted.
4. **Sign-in starts with the e-mail** everywhere: `POST /api/v3/auth/verify-email` returns
   `providers` and `has_created_password` (ADR 0134).
5. **Sign-in magic link** for logins without a password: one hour, single use, resend after 60
   seconds. "Forgot password" without a password sends it too.
6. **Apple token lifecycle**: refresh token stored encrypted, revoked on account deletion, and an
   endpoint for Apple's server-to-server notifications.
7. **Pending login linking**: a provider sign-in blocked by a document conflict links, with a code, to
   the account that owns the document (ADR 0136).
8. Every login records `user_metadata.provider` and `user_metadata.is_private_email`.
9. Production launch values: Google **on**, Apple **on**, Microsoft **off**. The release becomes
   **mandatory** once it is published in both stores.
10. The documentation is consolidated: categories `auth`, `idp`, `oauth` and `horizon-authenticate`
    become category `authenticate`, with 13 initiatives (ADR 0131).

> [!NOTE]
>
> **Pending.** As of 2026-11-04 every 2026.11.3 task was merged in PR #1960, not merged. The manual
> test table MT-01 to MT-11 is pending, and turning the flags on in production, the App Review
> submission and the mandatory release (T-15 to T-17) are still to do.

## Status per capability

Progress percentages from each initiative's `tracker.yml` **as of 2026-11-04**. When the last audit is
older, its date is shown.

| Capability | Initiative | Progress | What already works | What is missing |
| - | - | - | - | - |
| Registration | `registration` | 90 % | Progressive v3 registration (POST and PATCH), e-mail verification, identity path 1 and 2, account migration | Attach the account on the anonymous POST only with an attested RUT (T-117), unify \~190 `user_dni` rows to `CHL_RUT` |
| E-mail-first sign-in | `sign-in` | 58 % | Password sign-in and `verify-email` | E-mail first with `providers` in the app and the web (merged in #1960) |
| Providers | `oauth` | 33 % | Google and Microsoft browser start and callback routes | Apple, passwordless registration, per-provider flags, revocation (in review); spike T-01; certification tests; production launch |
| Magic link | `magic-link` | 15 % | Portal activation link (onboarding) | Sign-in magic link in the app and the web (in review) |
| Password | `password` | 13 % | Recovery and reset | Create and change password, portal banner, 4-of-5 rule everywhere (in review) |
| Session | `session` | 74 % (2026-10-14) | Horizon JWT, per-environment cookie, `bbx` handoff, app sign-out on a 401 | No tokens in query strings, `no-store` on every session response, lower `JWT_EXPIRES_IN` from 1 year to 30 days |
| Identity linking | `identity-linking` | 0 % | — | Everything merged in #1960; support badge in Horizon Enterprise (T-04) |
| Identity provider | `identity-provider` | 71 % (2026-09-23) | Protocol layer, exchange, RS256 for Odoo, management API and screens | Odoo only in development; client secret rotation, IdP rate limits and kill switch |
| Machine authentication | `machine-auth` | 59 % (2026-10-13) | `CRON_SECRET` guard, Maihue Lunch worker sign-in | Rotate `CRON_SECRET` in every environment (T-112) |
| Auth events | `auth-events` | 100 % (2026-09-23) | `auth.users` webhook and Send Email Hook | Authenticate the webhook caller, tracked in `security-hardening` |
| Security | `security-hardening` | 8 % | Rate-limit and lockout budgets in code, switchable by environment variable | Policies for the new routes, redirect allowlist, purge of token-bearing logs |
| Sign-in shell | `sign-in-shell` | 50 % (2026-09-23) | horizon-authenticate in `es` and `en`, 2FA gate (off) | Placeholder testimonial in the brand panel, environment contract test |
| Better Auth migration | `better-auth-migration` | 24 % (discovery, 2026-09-23) | Wave 0 on the current engine | Better Auth does not exist yet; a future engine change with no user-visible change |

> [!WARNING]
>
> There is **no second factor** today: the `authenticate-two-factor-ui` flag is off in every environment and the 2FA
> backend does not exist. When this documentation says "the 2FA gate", that step does nothing yet.

## See also

* [E-mail-first sign-in](https://docs.bbrands.io/maihuego/authentication/sign-in.md)
* [Step-by-step rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)
* [Authentication definition](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/definitions/authentication.md)
* [`authenticate` initiatives](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/initiatives/authenticate/README.md)


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