# Link several sign-ins to one account

Connect multiple Google, GitHub, Microsoft, Apple, Hugging Face, or ChatGPT accounts to one PocketBase record using cryptographic ownership proofs rather than matching email addresses.

![Ada signs in with Google, connects GitHub via an ownership proof, and ends up with both accounts linked to one user](assets/linking-flow.svg)

## Why email matching is blocked

Stock PocketBase auto-links a social login whenever its email matches an existing account, which lets an attacker with an unverified provider account take over existing users (called nOAuth on Microsoft multi-tenant apps). pbauth blocks implicit email matching across all linking scenarios:

| Situation                                                     | Outcome                                                                                  |
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| Email matches a user who already has linked accounts          | Rejected with `400` prompting sign-in through a linked account                           |
| Email matches a user with zero linked accounts                | Rejected with `400` unless `AllowExistingUnlinkedEmailMatch` is enabled                  |
| Microsoft email lacks domain verification (`xms_edov: false`) | Treated as unverified so login fails closed                                              |
| Brand-new user signup                                         | Evaluated by `Config.AllowEmail` and `Config.OnBeforeOAuthCreate` before writing records |

## Link another account

Signed-in visitors start a standard provider login in connect mode to obtain a 300s JSON Web Token (JWT) ownership proof without replacing their active session.

1. `<LinkedAccounts />` sends the visitor to `/api/auth/oauth/{provider}/start?mode=connect`.
2. After provider authorization, the ticket exchange returns `{ proof, owner, email, provider }` alongside any non-reserved fields added by `Config.OnAccountProof`, keeping the current PocketBase session active.
3. The client posts `{ proof }` (and optional JSON `extra`) to `/api/auth/accounts/connect`, where pbauth verifies the proof, enforces `MaxPerProvider`, saves the link, invokes `Config.OnAccountConnect` inside the transaction, and returns the updated account list.

Connect and unlink lifecycle behavior attaches to four server hooks configured in [the Go plugin options](providers.md#configure-the-go-plugin):

| Hook                  | Timing                                              | Use case                                                     |
| --------------------- | --------------------------------------------------- | ------------------------------------------------------------ |
| `OnBeforeOAuthCreate` | Before creating a brand-new user from OAuth         | Seed default roles or plan metadata on signup                |
| `OnAccountProof`      | When signing the connect proof JWT                  | Attach verified workspace claims or custom `meta` fields     |
| `OnAccountConnect`    | Inside the `/accounts/connect` SQL transaction      | Save org memberships or audit logs from `claims` and `extra` |
| `OnMerge`             | When connecting an identity owned by another record | Reassign child records before combining both accounts        |

## Combine accounts owned by someone else

Connecting a social account that already belongs to another record fails (`409 Conflict` when `Config.OnMerge` is `nil`, or `400 Bad Request` until confirmed with `merge: true`):

1. `LinkedAccounts` detects the ownership mismatch from `proof.owner` and passes `{ proof, accounts, confirmWith }` to its `onMergeConflict` callback so your UI can confirm the merge.
2. Once confirmed, `confirmWith` obtains a second ownership proof for one of the current user’s existing logins.
3. Calling `/api/auth/accounts/connect` with `merge: true` and both proofs executes `OnMerge(tx, current, other)` inside one database transaction and moves the external link cleanly.

## Remove a linked account

Both `DELETE /api/auth/accounts/{id}` and PocketBase’s built-in `DELETE /api/collections/_externalAuths/records/{id}` share two safety guards:

- **Ownership check**: Deleting a link owned by a different user returns `404`
- **Lockout check**: Deleting an account’s final sign-in returns `400` with `Keep at least one sign-in account connected.` unless `AllowUnlinkLastWhenPasswordSet` is enabled on a password-verified record

## Database schema changes

PocketBase’s default `idx_externalAuths_record_provider` index limits users to one external identity per provider. `pbauth.RegisterMigrations()` converts that index to non-unique, enforces global uniqueness per external account, adds an `email` column to `_externalAuths`, and backfills the address for users with a single linked login.
