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

# E-mail-first sign-in

> How sign-in always starts with the e-mail, what verify-email returns and which screen the app and the web show in each case.

From 2026.11.3, **every sign-in starts by asking for the e-mail**, in the MaihueGO app and in every
web app (which sign in through horizon-authenticate). With the e-mail, horizon-api answers which
methods that login has and the client shows only the ones that work: password, enabled providers or
magic link (ADR 0134). UI copy is quoted in Spanish, as the app shows it.

![Decision tree: the person types their e-mail and the client calls POST /api/v3/auth/verify-email, which returns the providers whose flag is on and has\_created\_password. An unknown e-mail goes to registration; a half-done registration only offers to finish it with its provider; an account with a password shows the password field and its providers; an account without a password linked to a provider shows the provider buttons and the magic link; a migrated account with neither only gets the magic link. 'Forgot password' on an account without a password sends the magic link with the same on-screen answer.](https://mintcdn.com/bbrands/-6vpv4Xb-mMAz8bx/images/authenticate/sign-in-routing.svg?fit=max\&auto=format\&n=-6vpv4Xb-mMAz8bx\&q=85\&s=82f22894171e44a63e7705d2999db86a)

What sign-in shows after the e-mail

## Step 1 · The e-mail: `POST /api/v3/auth/verify-email`

Public endpoint (permission `auth:verify-email`). The e-mail is normalised (lowercase, trimmed) and the
lookup is exact and fails closed: a database error answers `500`, never "does not exist" (ADR 0091).

```json Request theme={null}
{ "email": "person@example.com" }
```

```json Anonymous response (main fields) theme={null}
{
  "email": { "exists": true, "validated": true },
  "account": { "exists": true },
  "profile": { "complete": true },
  "has_created_password": false,
  "providers": ["google", "apple"]
}
```

| Field | Meaning |
| - | - |
| `email.exists` | A login with that e-mail exists. |
| `email.validated` | That login's e-mail is confirmed. |
| `account.exists` | The login already has an account (finished registration). |
| `has_created_password` | The login has a password. Computed as `user_metadata.has_created_password === true`, so a missing value counts as "no". |
| `providers` | The login's OAuth identities **whose flag is on**, in the order `google`, `apple`, `microsoft`. A flag that is off or could not be evaluated does not appear (fails closed). |

* A caller with a Horizon JWT whose confirmed e-mail owns the profile gets the **full payload**
  (account, profile, `dni_validated`, `has_subscription_product_history`, the account's `created_at`, etc.) plus `providers`.
* The temporary server flag `authenticate-verify-email-anonymous-profile` (formerly the
  `AUTH_VERIFY_EMAIL_ANONYMOUS_PROFILE` variable), `true` in every environment until 2026-11-14,
  returns the full payload to everyone while older app versions need it; each use is logged as `verify_email_anonymous_profile_compat`. The
  production target is `false`.

## Step 2 · The route table

The decision is a pure, unit-tested function on both clients (`utils/verify-email-routing.ts` in the
app, the e-mail step action in horizon-authenticate).

| What `verify-email` says | What is shown |
| - | - |
| No login | Registration (in the app, the e-mail confirmation step that opens registration; on the web the e-mail travels sealed in a short-lived `?prefill=` token, never in clear in the URL) |
| Login with an unconfirmed e-mail | "E-mail sent": the account must be validated from the e-mail |
| OAuth identity without an account (half-done registration) | Only "Termina tu registro con `<Proveedor>`" |
| Account with a password | Password field and "o continúa con `<Proveedor>`" for each enabled provider |
| Account without a password, with providers | "Este correo no tiene contraseña", provider buttons and "Enviarme un enlace" |
| Account with neither password nor providers (migrated, contract signer) | Only "Enviarme un enlace" |

Two edge cases in the app:

* Confirmed login, no account and **with** a password (half-done e-mail registration): the app carries
  on with registration (document validation).
* Confirmed login, no account, no password and its provider's flag is off: the app stays on the
  welcome screen, because there is no method to offer.

> [tab] MaihueGO app
>
> * The routes after the e-mail are `email-sent`, `confirm-email`, `login-full` or `login-anonymous`
>   (with password), `sign-in-methods` in `passwordless` mode (no password) or
>   `finish-registration` mode (half-done registration), `validate-dni` and `stay`.
> * The methods screen (`auth.sign-in-methods`) replaces the old "stay" and "continue with Google"
>   screens.
> * "Enviarme un enlace" leads to "Revisa tu correo": "Te enviamos un enlace para entrar a MaihueGO.
>   Vence en 1 hora y sirve una sola vez." Resend unlocks after 60 seconds.
> * When the app reopens without a session, the same logic decides where to resume.

> [tab] Web (horizon-authenticate)
>
> * The e-mail step is a server action that works even without JavaScript.
> * Four branches after the e-mail: password with "o continúa con", methods ("Este correo no tiene
>   contraseña"), "Termina tu registro" and link only. They are covered by Playwright.
> * If `verify-email` does not answer, answers empty or answers something unexpected, the web **falls
>   back to the password step** so nobody is blocked.
> * After the password, the provider or the magic link comes the 2FA gate and then the redirect to
>   `next`.

If `verify-email` fails in the app, a generic error is shown and the typed e-mail is kept.

> [!WARNING]
>
> The 2FA gate is **inert** today: the `authenticate-two-factor-ui` flag is off in every environment and there is no
> 2FA backend. The web only asks for the second step if the backend answers `requires_2fa`.

## "Forgot password" without a password

A login without a password cannot recover one. In that case `POST /api/v3/auth/recovery-account` sends
the **sign-in magic link** instead of the recovery e-mail, with the same answer and the same on-screen
message. That way the screen does not reveal whether the account has a password. In the app:

| Situation | App route |
| - | - |
| Unknown e-mail | `unknown-email` |
| Unconfirmed e-mail | `email-sent` |
| With password | `recovery` (Supabase reset e-mail) |
| Account without password | `magic-link` (sign-in link) |
| No account and no password (half-done OAuth registration) | `no-password`: asks to finish registration with the provider |

Details in [Password](https://docs.bbrands.io/maihuego/authentication/password.md) and
[Magic link](https://docs.bbrands.io/maihuego/authentication/magic-link.md).

## Step 3 · Password sign-in: `POST /api/v3/auth/login/password`

Permission `auth:login-password`. Body `{ "email": "…", "password": "…" }`.

| Result | Response |
| - | - |
| Success | `token` (Horizon JWT), `token_ws` and `refresh_token_ws` (Supabase tokens), `account`, `accounts[]` with their roles and `user` (picture, sex, `courtesy_recharge_limit`, `courtesy_recharge_used`) |
| Deferred registration | The same payload with `account: null` and `accounts: []` |
| Wrong credentials | `401` |
| Lockout | With the `authenticate-login-lockout` flag on, 5 consecutive failures lock access for 15 minutes |

If the login still had `has_created_password` set to false, a successful password sign-in fixes it
(best-effort). Every sign-in method ends in the same service (`LogicCommonService.loginCommon`), so the
payload is the same for password, provider and magic link.

## Limits and the person's IP

| Bucket | Limit | Window | Per |
| - | - | - | - |
| `verify-email` | 30 | 10 min | IP |
| `login-password` | 10 | 10 min | IP (plus lockout) |
| `profile-me` | 60 | 1 h | user |

horizon-authenticate calls `verify-email`, `magic-link/request`, `login/password` and recovery from the
server, so every call leaves from the same IP. That is why it sends the person's real IP in
`x-auth-client-ip` together with `x-auth-bff-token`, and horizon-api only counts by that IP when the
token matches its `AUTH_BFF_SERVER_TOKEN` (ADR 0098). The IP is read from `x-vercel-forwarded-for` or
`x-real-ip`.

> [!WARNING]
>
> If `AUTH_BFF_SERVER_TOKEN` is not the same in horizon-api and horizon-authenticate, every sign-in in
> the environment shares the server IP budget and `verify-email` answers `429` to everyone as soon as a
> handful of people sign in.

## Accepted risk: enumeration

`providers` tells, for a given e-mail, which enabled provider it signs in with. That is extra
information about whether an account exists. It was accepted on purpose (ADR 0134): the endpoint is
rate-limited and returns no profile data to anonymous callers.

## See also

* [Registration](https://docs.bbrands.io/maihuego/authentication/registration.md)
* [Google, Apple and Microsoft](https://docs.bbrands.io/maihuego/authentication/oauth.md)
* [Magic link](https://docs.bbrands.io/maihuego/authentication/magic-link.md)
* [Security](https://docs.bbrands.io/maihuego/authentication/security.md)
* [ADR 0134](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/decisions/0134-identifier-first-sign-in-and-sign-in-magic-link.md)


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