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.
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.
<LinkedAccounts />sends the visitor to/api/auth/oauth/{provider}/start?mode=connect.- After provider authorization, the ticket exchange returns
{ proof, owner, email, provider }alongside any non-reserved fields added byConfig.OnAccountProof, keeping the current PocketBase session active. - The client posts
{ proof }(and optional JSONextra) to/api/auth/accounts/connect, where pbauth verifies the proof, enforcesMaxPerProvider, saves the link, invokesConfig.OnAccountConnectinside the transaction, and returns the updated account list.
Connect and unlink lifecycle behavior attaches to four server hooks configured in the Go plugin options:
| 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):
LinkedAccountsdetects the ownership mismatch fromproof.ownerand passes{ proof, accounts, confirmWith }to itsonMergeConflictcallback so your UI can confirm the merge.- Once confirmed,
confirmWithobtains a second ownership proof for one of the current user’s existing logins. - Calling
/api/auth/accounts/connectwithmerge: trueand both proofs executesOnMerge(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
400withKeep at least one sign-in account connected.unlessAllowUnlinkLastWhenPasswordSetis 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.