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

# Google, Apple and Microsoft

> How provider sign-in works on iOS, Android and the web, how the flags control it and what contracts its endpoints have.

MaihueGO and the web apps offer three providers: **Google**, **Apple** and **Microsoft**. Supabase Auth
is the OAuth client towards each provider; horizon-api wraps the flow, creates the profile when needed
and issues the session. Each provider appears only when its flag is on. Console configuration is in
[Rollout](https://docs.bbrands.io/maihuego/authentication/setup.md) and the Apple token lifecycle in
[Apple: tokens and notifications](https://docs.bbrands.io/maihuego/authentication/apple.md).

![On the left, the native Apple flow on iOS: Sign in with Apple button, Apple returns the identity token and the authorization code, the app sends them with the nonce to POST /api/v3/auth/login/oauth/apple, horizon-api signs in to Supabase with signInWithIdToken and stores Apple's refresh token encrypted. On the right, the browser flow for Android and the web: the app or the web starts the flow, Supabase authorize and the provider consent, the Supabase and horizon-api callbacks, the return to the app deep link or to horizon-authenticate, and oauth/complete. Each provider only appears when its flag is on.](https://mintcdn.com/bbrands/-6vpv4Xb-mMAz8bx/images/authenticate/oauth-provider-flows.svg?fit=max\&auto=format\&n=-6vpv4Xb-mMAz8bx\&q=85\&s=7b375c2d16ab4be6c2650a259359ef02)

Two ways to sign in with a provider

## Behaviour per platform

| Platform | Google | Apple | Microsoft |
| - | - | - | - |
| iOS | Browser (system authentication session) | **Native** (`AppleAuthenticationButton`) | Browser |
| Android | Browser | Browser (Custom Tabs) with the Services ID | Browser |
| Web | Browser through `oauth/start` | Browser through `oauth/start` with the Services ID | Browser through `oauth/start` |

> [tab] iOS · native Apple
>
> **1. Nonce**
>
> The app generates a random nonce with `expo-crypto` and sends its SHA-256 to Apple.
>
> **2. Native sheet**
>
> Apple returns `identity_token`, `authorization_code` and, **only on the first authorization**,
> the first and last name.
>
> **3. horizon-api**
>
> The app calls `POST /api/v3/auth/login/oauth/apple` with the raw nonce. horizon-api signs in
> with `signInWithIdToken`, exchanges the `authorization_code` and stores the refresh token
> encrypted.
>
> **4. Result**
>
> Existing account → Home. New login → registration without a password (see
> [Registration](https://docs.bbrands.io/maihuego/authentication/registration.md)).
>
> The Apple button has the same height and width as the Google one; the old "Apple no disponible"
> toast is gone.

> [tab] App · browser
>
> **1. Start**
>
> `GET /api/v3/auth/login/oauth/google` (or `apple`, `microsoft`) with
> `redirect_url=<scheme>://…` and `format=json` → `{ "redirectUrl": "…" }`.
>
> **2. Provider**
>
> The app opens that URL in the system browser. Google requests `access_type=offline` and
> `prompt=select_account consent`; Microsoft requests the `email` scope and
> `prompt=select_account`.
>
> **3. Callback**
>
> horizon-api's callback page reads the URL fragment and POSTs it to `callback-redirect`.
>
> **4. Deep link**
>
> `callback-redirect` answers `302/303` to the app deep link with the tokens **in the fragment**
> (`#access_token=…`), plus `is_new_user`, `has_created_password` and `needs_name`.

> [tab] Web
>
> **1. Buttons**
>
> horizon-authenticate evaluates the flags on the server and shows only the enabled providers.
>
> **2. Start**
>
> The button calls the internal route `api/auth/oauth/[provider]/start`, which asks horizon-api for
> `GET /api/v3/auth/oauth/start` with `provider`, `redirect_to` and `state`, and receives
> `auth_url`.
>
> **3. Return**
>
> The provider returns to `auth/oauth/callback`, which calls `api/auth/oauth/complete`, which calls
> `POST /api/v3/auth/oauth/complete`.
>
> **4. Welcome and destination**
>
> With `needs_terms` or `needs_name`, `auth/oauth/welcome` is shown; then the 2FA gate (inert
> today) and the redirect to `next`.
>
> A provider whose flag is off is refused even if someone types its route by hand (covered by E2E
> tests).

## The provider footer in MaihueGO

* Buttons are always ordered Google, Apple, Microsoft, and only the enabled ones are shown, packed
  together with no gaps.
* The 240 pt slot is held even when there are no buttons, so the screen does not jump.
* With every provider off, the block stays mounted at `opacity:0`, is not tappable and is hidden from
  screen readers.
* While a sign-in is in progress, every button is disabled.
* During sign-in an "entering the app" overlay is shown. Home, the courtesy screen, the Android back
  button or a 20-second failsafe timer dismiss it.

## Per-provider flags

| Flag | Category | Type | Default | Production at launch |
| - | - | - | - | - |
| `authenticate-oauth-google` | `authenticate` | boolean | `false` | on |
| `authenticate-oauth-apple` | `authenticate` | boolean | `false` | on |
| `authenticate-oauth-microsoft` | `authenticate` | boolean | `false` | **off** |

One flag applies to **every platform** (ADR 0133). The old keys `maihuego-application-oauth-apple`,
`-google` and `-microsoft` are deleted by the Capacidades Sync in Horizon Enterprise; app versions older
than 2026.11.3 do not read the new keys and show no provider until the forced update moves them.

> [accordion] How the app evaluates them
>
> * `GET /api/v3/feature-flag/evaluate?category=authenticate` (public, answers a key-to-boolean map),
>   on mount and when returning to the foreground, except during an OAuth flow in progress.
> * It stores the **last successful evaluate** in AsyncStorage; if an evaluate fails, it keeps the
>   previous one. A missing key counts as off.
> * It never sends `FEATURE_FLAG_SERVER_TOKEN`: only the `Accept` and `Cache-Control: no-store`
>   headers.
> * Buttons are hidden without unmounting the block.

> [accordion] How the web evaluates them
>
> horizon-authenticate evaluates on the server and only renders the enabled buttons. horizon-api also
> filters `verify-email`'s `providers` with the same flags (`enabledOauthProviders()`), which fail
> closed: an evaluation error counts as off.

> [accordion] Clean install and effect of a change
>
> A clean install, or the first launch after updating, **shows no provider** until the first
> successful evaluate. A flag change shows up at the **next evaluate**, without shipping anything. If a
> tester still sees the old state, they should force-close the app and open it again.

## Everyday cases

| Case | What happens |
| - | - |
| Returning person with an account | The app calls `POST /api/v3/auth/oauth/complete`, stores the session `profile/me` returns and opens Home. If `profile/me` fails, it uses a fallback session with the JWT and the unverified claims (e-mail, `sub`) and no accounts. |
| Cancel at the provider | Back on the same step, no session, e-mail kept. A Google `access_denied` deep-links only that error. |
| Apple without e-mail on later sign-ins | The login is found by the Apple identifier (`sub`); nothing is asked again. |
| Apple "Hide My Email" | The `@privaterelay.appleid.com` address becomes the login e-mail and `is_private_email=true`. |
| E-mail not verified at the provider | No profile is created; the deep link returns `error=unverified_email`. For Microsoft, the e-mail only counts as verified from `email_confirmed_at` or from the Azure identity of the same address. |
| Document already registered by another account | Conflict screen with "Es mi cuenta, vincular" (see [Identity linking](https://docs.bbrands.io/maihuego/authentication/identity-linking.md)). |

Each callback accepts only a session that holds **its provider's identity** (`google` for Google,
`azure` for Microsoft); `app_metadata.provider` is not enough.

## Endpoint contracts

| Route | Contract |
| - | - |
| `POST /api/v3/auth/login/oauth/apple` | Body: `identity_token` (≤ 8192), `nonce` (≤ 512), `authorization_code` (≤ 4096), optional `given_name` and `family_name`. Answers `access_token`, `refresh_token`, `email`, `is_new_user`, `has_created_password`, `needs_name`, no cache. Wrong nonce or audience → error, no session; refused identity → `401 invalid-apple-identity`. |
| `GET /api/v3/auth/login/oauth/google`, `/apple`, `/microsoft` | `redirect_url` required (`400 redirect_url is required`). With `format=json` or `Accept: application/json` → `200` with `redirectUrl`; otherwise `302`. |
| `GET …/callback` | HTML page that reads the fragment and POSTs it to `callback-redirect`. |
| `GET` or `POST …/callback-redirect` | Requires `access_token`. Custom scheme → `302/303` to the deep link with tokens in the fragment; http(s) → JSON payload. Tokens in a GET query → `410` (Microsoft always; the others while the `authenticate-oauth-query-tokens-accepted` flag is `false`, error `OAUTH_CALLBACK_UPGRADE_REQUIRED`). Bad body → `400 invalid_oauth_callback_body`. |
| `POST /api/v3/auth/oauth/register` | Supabase tokens, optional `password` (4 of 5) and path 2 profile data. Without a password → `has_created_password=false`. `409 password-already-created`. |
| `GET /api/v3/auth/oauth/start` | Query `provider`, `redirect_to`, `state` → `auth_url`. |
| `POST /api/v3/auth/oauth/complete` | Tokens in the JSON body only. New login without `user_metadata` → profile and person account. Answers `jwt`, `is_new_user`, `needs_terms`, `needs_name` unwrapped. |
| `POST /api/v3/auth/oauth/welcome` | Horizon JWT. `terms_accepted: true`, optional `firstname` and `lastname` (≤ 250). |
| `POST /api/v3/auth/oauth/apple/notifications` | Apple notifications; see [Apple](https://docs.bbrands.io/maihuego/authentication/apple.md). |

The start, callback and native Apple routes are public, each with its own rate-limit policy.
`oauth/register`, `oauth/complete` and `oauth/welcome` require the session of the login they act on.

> [!NOTE]
>
> **In progress.** The `redirect_url` allowlist exists per environment
> (`config/social-login/redirects.<environment>.json`) behind the server flag
> `security-social-redirect-enforce` (it replaced `AUTH_SOCIAL_REDIRECT_ENFORCE` in 2026.11.2); with
> `false` it only logs. The 2026.11.2 Sync seeds it `true` in every environment. Making it mandatory is tracked in `security-hardening` (T-105).

## Limits

| Bucket | Limit | Window | Per | If Redis is down |
| - | - | - | - | - |
| `oauth-start` | 30 | 10 min | IP | local limit |
| `oauth-welcome` | 10 | 10 min | user | local limit |
| `oauth-link` | 5 | 1 h | login and IP | closed |
| `apple-notifications` | 20 | 10 min | Apple identifier, never IP | local limit |

## See also

* [Apple: tokens and notifications](https://docs.bbrands.io/maihuego/authentication/apple.md)
* [Registration](https://docs.bbrands.io/maihuego/authentication/registration.md)
* [Step-by-step rollout](https://docs.bbrands.io/maihuego/authentication/setup.md)
* [Operations and maintenance](https://docs.bbrands.io/maihuego/authentication/operations.md)
* [ADR 0133](https://github.com/bbrandslab/bbrands-monorepo/blob/develop/docs/decisions/0133-one-feature-flag-per-oauth-provider.md)


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