pbauth / Docs

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.

Linking a second sign-in Step 1: Ada is signed in with Google and clicks Connect GitHub. Step 2: the GitHub sign-in returns a 5-minute proof without switching her session. Step 3: her account now has two sign-ins, Google and GitHub. 1. SIGNED IN Ada clicks Connect GitHub Googleada@gmail.comHer session stays as it is 2. PROOF GitHub sign-in returns a proof GitHubada@users.noreply…A 5-minute token, no session switch 3. LINKED One account, two sign-ins Googleada@gmail.comGitHubada@users.noreply…
Ada signs in with Google, connects GitHub via an ownership proof, and ends up with both accounts linked to one user

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:

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

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:

HookTimingUse case
OnBeforeOAuthCreateBefore creating a brand-new user from OAuthSeed default roles or plan metadata on signup
OnAccountProofWhen signing the connect proof JWTAttach verified workspace claims or custom meta fields
OnAccountConnectInside the /accounts/connect SQL transactionSave org memberships or audit logs from claims and extra
OnMergeWhen connecting an identity owned by another recordReassign 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.