Configuration
You can enable OAuth2 authentication in your auth collection settings:Provider configuration
Each OAuth2 provider requires specific configuration:For standard providers (Google, GitHub, etc.), you only need to provide the
name, clientId, and clientSecret. The URLs are automatically configured.Supported providers
PocketBase supports 15+ OAuth2 providers out of the box:- GitHub
- GitLab
- Discord
- Microsoft
- Spotify
- Kakao
- Twitch
- Strava
- Gitee
- LiveChat
- Gitea
- OIDC (OpenID Connect)
- Apple
- VK
- Yandex
- Patreon
OAuth2 flow
The OAuth2 authentication process follows these steps:1
Get auth methods
Retrieve available OAuth2 providers and their authorization URLs.Response:
2
Redirect to provider
Redirect the user to the provider’s authorization URL with your redirect URI appended.
3
Handle callback
After user authorizes, the provider redirects back with an authorization code.
4
Exchange code for token
Send the authorization code to PocketBase to complete authentication.
5
Receive auth token
PocketBase returns the auth token and user record.
Authentication endpoint
POST/api/collections/{collection}/auth-with-oauth2
Field mapping
You can map OAuth2 user data to your collection fields:If the mapped field for
avatarURL is a file field, PocketBase automatically downloads the avatar image. Otherwise, it stores the URL as a string.New user creation
When a user authenticates with OAuth2 for the first time, PocketBase creates a new record. You can provide additional data:PKCE (Proof Key for Code Exchange)
PocketBase supports PKCE for enhanced security. PKCE is automatically enabled for providers that support it:You can override the default PKCE setting for any provider using the
pkce field in the provider configuration.Existing user linking
When a user authenticates with OAuth2, PocketBase tries to link the OAuth2 account to an existing user in this order:1
Check for existing OAuth2 link
Search for an existing
ExternalAuth record with the same provider and provider ID.2
Use authenticated user
If the user is already authenticated (e.g., linking additional OAuth2 account), use the current auth record.
3
Match by email
If the OAuth2 user has an email, search for an auth record with that email.
4
Create new user
If no existing user is found, create a new auth record.
Email verification
OAuth2 authenticated users are automatically verified if their email matches the OAuth2 provider’s email:Provider-specific notes
Apple
Apple returns the user’s name only during the first authorization. PocketBase handles this by storing the name temporarily:response_mode=form_post:
LinkedIn OIDC
Some providers like LinkedIn OIDC may require manual PKCE adjustment:Security considerations
Best practices
- Use HTTPS: Always use HTTPS for redirect URLs in production.
- Validate state parameter: Although PocketBase handles this internally, ensure your client validates the state parameter to prevent CSRF attacks.
- Store secrets securely: Never expose OAuth2 client secrets in client-side code.
- Limit provider scopes: Only request the minimum required OAuth2 scopes.
- Handle email conflicts: Consider what happens when an OAuth2 email matches an existing password-authenticated user.
Custom OAuth2 hooks
You can customize OAuth2 authentication behavior:OAuth2 redirect handler
PocketBase provides a global OAuth2 redirect handler for subscription redirects:Related topics
Email/Password auth
Set up traditional password authentication
MFA
Require multiple auth methods
OTP authentication
Enable one-time password login
API rules
Configure access control