pbauth / Docs

Set up sign-in providers

Enable six social OAuth2 providers, email and password login, and 6-digit One-Time Password (OTP) flows using environment variables or Go configuration without manual PocketBase admin setup.

Sign-in options pbauth supports Google, Microsoft, GitHub, Apple, Hugging Face and ChatGPT sign-in, plus email and password. Google Microsoft GitHub Apple Hugging Face ChatGPT Email
Google, Microsoft, GitHub, Apple, Hugging Face, ChatGPT and email sign-in

Important

Every provider sends returning visitors to https://your_app.example/api/oauth2-redirect. Register that URL in each provider app alongside http://127.0.0.1:8090/api/oauth2-redirect for local development.

Social providers

Create an OAuth client with each provider you support and export its environment variables before starting PocketBase. Any provider without credentials stays hidden from AuthCard.

ProviderEnvironment variablesWhere to create the client
GoogleGOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRETGoogle Auth Platform web application client
MicrosoftMICROSOFT_CLIENT_ID, MICROSOFT_CLIENT_SECRETMicrosoft Entra app registrations web platform
GitHubGITHUB_CLIENT_ID, GITHUB_CLIENT_SECRETGitHub App with Email addresses: Read-only or an OAuth App
AppleAPPLE_CLIENT_ID plus a .p8 key (below)Apple Developer App ID, Services ID, and key
Hugging FaceHF_CLIENT_ID, HF_CLIENT_SECRETHugging Face Connected Applications with openid, profile, and email scopes
ChatGPTCHATGPT_CLIENT_ID, CHATGPT_CLIENT_SECRETOpenAI OAuth client with your redirect URL

Hugging Face also reads HUGGINGFACE_CLIENT_ID and HUGGINGFACE_CLIENT_SECRET, and ChatGPT also reads OPENAI_OAUTH_CLIENT_ID and OPENAI_OAUTH_CLIENT_SECRET. Each provider expects these extra settings:

  • Microsoft: Add the optional ID token claims email and xms_edov under Token configuration so pbauth rejects unverified work emails and blocks nOAuth pre-hijacking
  • GitHub: Grant email read permission so pbauth reads verified primary addresses from /user/emails
  • Hugging Face and ChatGPT: Leave OpenID Connect (OIDC) endpoints unconfigured because pbauth wires them into PocketBase’s oidc and oidc2 slots and checks ID tokens against published keys

Apple .p8 key

Sign in with Apple uses a JSON Web Token (JWT) client secret that expires every 180 days. Supply your .p8 private key so pbauth signs a 150-day secret at startup and rotates it weekly via AppleRefreshCron:

VariableValue
APPLE_CLIENT_IDServices ID such as com.example.web
APPLE_TEAM_ID10-character Apple Team ID
APPLE_KEY_ID10-character Sign in with Apple Key ID
APPLE_PRIVATE_KEYContents of your .p8 file (escaped \n newlines are normalized automatically)

A fixed APPLE_CLIENT_SECRET still works if you prefer to rotate secrets outside PocketBase.

Email and OTP

Call pbauth.EmailAuthFromEnv() for Config.EmailAuth to configure password sign-in, 6-digit email OTP verification, and password reset from environment variables:

VariableDefaultEffect
PBAUTH_EMAIL_AUTHfalseEnables email and password sign-in
PBAUTH_EMAIL_SIGNUPfalseAllows new account registration by email
PBAUTH_MIN_PASSWORD_LENGTH8Minimum accepted password length
PBAUTH_DISABLE_EMAIL_VERIFICATIONfalseLets unverified accounts sign in
PBAUTH_DISABLE_PASSWORD_RESETfalseDisables password reset flows
PBAUTH_ALLOW_EMAIL_CHANGEfalseLets signed-in accounts change their email
PBAUTH_VERIFICATION_MODE"otp"Selects "otp" 6-digit codes or "link" emails
PBAUTH_PASSWORD_RESET_MODE"otp"Selects "otp" 6-digit codes or "link" emails
PBAUTH_OTP_LOGINfalseTurns on passwordless email OTP sign-in
PBAUTH_OTP_LENGTH6Numeric code length, clamped between 6 and 10
PBAUTH_OTP_DURATION_SECONDS300Code expiration lifetime in seconds

Signup verification and password reset default to "otp" mode with strict purpose scoping so verification codes can’t replay on passwordless or reset routes:

FlowRequest and confirm endpointsSecurity behavior
Email verificationPOST /api/auth/otp/verify/request, /otp/verify/confirmChecks the signup password alongside the OTP to stop pre-hijacking
Password resetPOST /api/auth/otp/reset/request, /otp/reset/confirmSets the new password inside the same confirmation request
Passwordless loginPocketBase requestOTP and authWithOTPEnabled when EnablePasswordlessOTP or PBAUTH_OTP_LOGIN is on

Tip

Built-in OTP and link emails localize across en, tr, ar with Right-to-Left (RTL) layout, es, fr, and de from Accept-Language or LocaleFromRequest. Override any message template with EmailAuthConfig.RenderEmail, and see verify and reset with email OTP for the React UI.

Set these outgoing mail server (SMTP) variables to deliver verification and reset messages:

VariableEffect
PUBLIC_URLBase URL used inside link-mode confirmation emails
MAIL_FROMSender header such as Acme <hi@acme.dev>
SMTP_URLsmtp://user:pass@host for STARTTLS on port 587, or smtps://user:pass@host for TLS on port 465

pbauth enforces native PocketBase rate limits of 10 password login attempts per minute and 5 verification or reset emails per minute. When Config.AllowEmail is set, it also gates email signup, verification, password reset, and email changes.

How same-tab sign-in works

Social sign-in runs inside the visitor’s starting tab with Proof Key for Code Exchange (PKCE) S256 so popup blockers and embedded browsers can’t interrupt login.

pbauth same-tab OAuth2 and account linking flow Your app sends the tab to GET /api/auth/oauth/{provider}/start, pbauth redirects to the provider with PKCE S256 and an HttpOnly state cookie, the provider returns to /api/oauth2-redirect, pbauth redirects back with a single-use ticket, and the app exchanges it at POST /api/auth/oauth/exchange or links it through the proof-verified POST /api/auth/accounts/connect. Email and password auth and every provider identity land on one canonical users record with several _externalAuths rows. Same-tab OAuth2 with PKCE The default flow. flow: "popup" keeps PocketBase’s popup flow instead. API call browser redirect Your app, same tab <AuthCard /> · <LinkedAccounts /> pbauth on PocketBase Go · one binary · SQLite OAuth2 provider 1 GET /api/auth/oauth/{provider}/start return_to · mode · login_hint or prompt 2 302 to provider · PKCE S256 HttpOnly, SameSite=Lax state cookie 3 GET or POST /api/oauth2-redirect code and state, Apple via form_post 4 303 return_to#pbauth_ticket=… single use, 60 s · errors: ?auth_error= 5 POST /api/auth/oauth/exchange login: { token, record } · connect: proof 6 POST /api/auth/accounts/connect proof-verified link · 409 or OnMerge Email and password sign up · verify email · reset password authWithPassword rate-limited sign-in and email requests users one canonical record per person ada@gmail.com · verified never merged by email alone _externalAuths google ada@gmail.com google ada@work.com github ada@users.noreply… several per provider, each identity once PocketBase email auth, same users
Same-tab sign-in from provider redirect to session exchange
  1. Start: GET /api/auth/oauth/{provider}/start verifies that return_to stays on your origin (over 3072 bytes it keeps only the path, or / when the path alone is longer), creates a PKCE S256 challenge with a 600s state, and binds it to an HttpOnly cookie scoped to /api/oauth2-redirect.
  2. Callback: The provider redirects back to /api/oauth2-redirect, where pbauth verifies the cookie and redirects to return_to with a one-time ticket in the URL fragment (or ?auth_error= on failure). PocketBase’s activity log skips successful start and callback requests, since their URLs carry the login_hint email and the provider’s code.
  3. Exchange: The React client posts the fragment ticket to /api/auth/oauth/exchange within 60s to receive a PocketBase session.

Apple posts its callback as an HTML form, so pbauth converts that POST into a 303 redirect to /api/oauth2-redirect before running the cookie check. PocketBase’s legacy popup mode keeps working when a callback state lacks the pbauth_ prefix (flow: "popup").

Provider names in the API

PocketBase stores Hugging Face in oidc and ChatGPT in oidc2, while pbauth exposes canonical keys in HTTP responses and accepts either form in Config:

ProviderKey in pbauthName in PocketBase
Hugging Facehuggingfaceoidc
ChatGPTchatgptoidc2
Google, Microsoft, GitHub, AppleSame in bothSame in both

Call pbauth.CanonicalProviderKey and pbauth.PocketBaseProviderName when converting between the two forms in Go.

Configure the Go plugin

Every field on pbauth.Config is optional when configuring via code or mixing hooks with environment defaults:

FieldDefaultPurpose
Collection"users"PocketBase auth collection managed by pbauth
AllowedProvidersall 6 providersRestricts accepted social providers for the collection
Providersloaded from envProgrammatic provider overrides (custom scopes via provider.Scopes() stay intact)
MaxPerProviderunlimitedCaps linked identities per provider (0 blocks linking that provider)
EmailAuthoffEmail, password, OTP, and template configuration (EmailAuthFromEnv())
SessionDurationcollection defaultCustom authentication token lifetime
RoutePrefix"/api/auth"Base path prefix for pbauth API routes
ExposeMethodsRoutefalseServes GET your_route_prefix/methods to signed-out visitors
AllowEmailnonePredicate deciding which email addresses can sign in or register
IsAuthorizedUsernoneRuns on every session, sign-in, and linked-account route
AllowExistingUnlinkedEmailMatchfalseLets a first OAuth login claim an existing unlinked record matching the email
AllowUnlinkLastWhenPasswordSetfalseLets accounts with a verified password remove their final social login
OnUserSyncnoneSyncs provider profile data onto the record after OAuth login
OnBeforeOAuthCreatenoneReturns e.CreateData fields for brand-new OAuth signups after AllowEmail passes
OnAccountProofnoneAdds custom non-reserved JWT claims and meta fields during proof creation
OnAccountConnectnoneRuns inside the connect database transaction with verified claims and optional JSON extra
OnOAuthSuccessnonePost-login hook running after OnUserSync completes
OnMergenoneCombines two user records in account merge flows
ProofHeadersX-Account-Proof, X-PBAuth-Account-ProofRequest headers switching an OAuth login into account-proof mode
ProofSecretSuffix"pbauth-account-proof"Suffix appended to token keys when signing linking proofs
ProofTTL300sLifetime of an ownership-proof JWT
DisableCOOPMiddlewarefalseSkips setting Cross-Origin-Opener-Policy headers
AppleRefreshCron"0 3 * * 0"Cron schedule for rotating Apple .p8 client secrets

Reserved proof claim keys (provider, identity, email, owner, exp, iat, nbf, sub) and reserved metadata keys (proof, owner, email, provider) are protected against overrides in OnAccountProof.