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

# Linking by RUT

> How a Google, Apple or Microsoft login joins the account that already owns its RUT, with a code sent to the owner's masked e-mail, and what each login records.

A **pending login** is a login with no active `account_user`: someone signed in with a provider and has
not finished registration yet. If during registration they declare a RUT or document that already
belongs to another account, they used to be stuck. From 2026.11.3 they can **link** to that account by
proving it is theirs (ADR 0136). It is not a merge: the existing account stays as it is and gains one
more login.

The typical case is Apple with «Hide My Email»: the `@privaterelay.appleid.com` address almost never
matches the account's e-mail, so the RUT is the key that finds it.

![A first sign-in with Google, Apple or Microsoft creates a pending login. Registration asks for the RUT. If another account already owns that document, the conflict screen shows the owner's masked e-mail. If the person confirms it is their account, the API starts the link, sends a code to the account's e-mail, the person confirms it and the login is linked with a new account\_user and the deferred profile completed. Then the person lands on Home of the existing account.](https://mintcdn.com/bbrands/-6vpv4Xb-mMAz8bx/images/authenticate/identity-linking.svg?fit=max\&auto=format\&n=-6vpv4Xb-mMAz8bx\&q=85\&s=bb50d3cc532dfe77c865250fc41987a6)

Linking a provider login to an existing account by RUT

> [!NOTE]
>
> **Status.** Initiative `authenticate/identity-linking` is in build. The endpoints, the columns and the
> MaihueGO screen ship in PR #1960 (merged into develop on 2026-11-05) with 2026.11.3, with no flag. The exit
> check is MT-04 with Google, Apple and Microsoft on iOS and Android.

## Step by step in MaihueGO

**1. Document conflict**

Registration answers `409` with `metadata.reason = DOCUMENT_ALREADY_REGISTERED` and the owner's
masked e-mail. The app shows the conflict screen (`auth.document-conflict`), which **keeps** that
masked e-mail visible.

**2. «Es mi cuenta, vincular»**

This is the primary action. The app calls `POST /api/v3/auth/oauth/link/start` with the pending
login's GoTrue tokens and the declared document.

**3. Code to the masked e-mail**

The code screen, reused from the account merge (`/account/merge`), reads «Te enviamos un código a»
followed by the owner's masked e-mail. The e-mail is the event `auth.login-link.code-requested`.

**4. Confirm**

`POST /api/v3/auth/oauth/link/confirm` with the code. When it is right the answer is
`{ linked: true }`.

**5. Enter the account**

The app finishes the provider sign-in with `oauth/complete`, now inside the linked account, and
opens its Home.

### «No tengo acceso a ese correo»

The secondary exit of the code screen opens identity verification. Its result (`identity_attestation`)
only counts as proof **when the owner account has no e-mail**, as in the assisted merge. If the account
has an e-mail, the code is the only valid proof.

> [!NOTE]
>
> **Exception to D-111.** In the assisted account merge (account-centric identity, D-111) the screen does
> not reveal the owner's e-mail. Here it shows it masked, by decision of ADR 0136, so the person
> recognises their account.

### E-mail and password registration

The same link works for an e-mail registration (account-centric identity D-175): the person confirmed
their e-mail with the registration code and is signed in, but has no account yet when the RUT turns out
to be taken. «Es mi cuenta, vincular» then calls the same routes with the session JWT in
`Authorization` and no GoTrue tokens. After the code, the app re-signs with `auth/profile/me` and opens
Home of the existing account; the new e-mail keeps its password as one more login of that account.

A registration that asks for the RUT before the e-mail exists has no login to link, so its conflict
screen offers only «Usar otro documento» and «Iniciar sesión con otro correo», and its text promises
nothing else.

## Contracts

| Method and path | Body | Response |
| - | - | - |
| `POST /api/v3/auth/oauth/link/start` | `{ access_token?, refresh_token?, type_dni, dni }` | Always `200 { sent: true }`, whether or not an account owns that document |
| `POST /api/v3/auth/oauth/link/confirm` | `{ access_token?, refresh_token?, type_dni, dni, code?, identity_attestation? }` | `200 { linked: true }` or `400 invalid-code` |

* **Authentication:** the pending login's GoTrue tokens, or, for the e-mail registration, its session JWT
  in `Authorization` with neither token in the body. Half a pair answers `400`.
* **`403 login-not-pending`:** the login already has a non-deleted `account_user` (active or not), or it
  came with GoTrue tokens and holds no provider identity.
* **`403 email-not-confirmed`:** a session login whose e-mail is not confirmed.
* **`400 invalid-code`:** wrong or expired code, refused attestation or a document without an account.
  Each attempt spends budget.
* **`429`:** budget exhausted; the screen asks to wait.
* Sending the code uses the assisted merge's 60-second resend window per account. When it cannot be
  sent (window taken, account without e-mail), the answer is still `{ sent: true }` and the reason goes
  to the logs.

## Budget

| Bucket | Limit | Dimensions | If the limiter is down |
| - | - | - | - |
| `oauth-link` | 5 per hour, shared by `start` and `confirm` | login and IP | closed (always enforced) |

## What linking writes

| Data | Change |
| - | - |
| `account_user` | New active row joining the pending login to the account that owns the document |
| The login's `user_metadata` | Takes from the owner's profile the fields it lacks: first name, last name, birthdate, sex and phone. Never replaces an existing value (best effort: if it fails, the link stays) |
| Owner account | Unchanged; the owner sees the new login among the account's logins |

## `provider` and `is_private_email` per login

Every login records how it was created and whether its e-mail is private. They are written by e-mail
registration and by `oauth/register`.

| Column (`user_metadata`) | Type | Values |
| - | - | - |
| `provider` | text, nullable | `email`, `google`, `apple`, `microsoft`, or empty for older logins |
| `is_private_email` | boolean, not null, default `false` | `true` when the e-mail is an Apple relay |

The migration is additive only and there is no backfill: logins created before 2026.11.3 keep an empty
`provider` and `is_private_email = false`.

## Support badge

> [!NOTE]
>
> **Pending.** Showing the provider and a private e-mail badge on a customer's logins in Horizon
> Enterprise is T-04 (p2, todo). Until then the data exists in `user_metadata` but is not visible in
> the support interface.

## See also

* [OAuth: Google, Apple and Microsoft](https://docs.bbrands.io/maihuego/authentication/oauth.md)
* [Apple](https://docs.bbrands.io/maihuego/authentication/apple.md)
* [Registration](https://docs.bbrands.io/maihuego/authentication/registration.md)
* [Security and limits](https://docs.bbrands.io/maihuego/authentication/security.md)


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