Google OAuth2 Setup Guide
This guide walks you through setting up Google OAuth2 sign-in for your PocketOne app.
1. Create/Select Google Cloud Project
- Go to Google Cloud Console
- Either select an existing project or create a new one
- If creating new, provide a project name when prompted

2. Configure OAuth Credentials
- Navigate to "Credentials" in the left sidebar
- Click "Create Credentials" and select "OAuth client ID"

Configure Application Type
- Select "Web application" as the application type
- Configure your authorized origins and redirect URIs:
- Authorized JavaScript origins: Your application domain (e.g.,
http://localhost:8090) - Authorized redirect URIs: Your auth callback URL (e.g.,
http://localhost:8090/auth/oauth/confirm)
- Authorized JavaScript origins: Your application domain (e.g.,

Configure Consent Screen (if needed)
If you haven't set up a consent screen before, you'll need to configure it:
- Choose External/Internal user type
- Fill in the required application information

Download Credentials
- After creating the OAuth client, download the JSON credentials file
- Store this file securely - it contains sensitive information

3. Configure OAuth2 in the dashboard
OAuth2 providers belong to an auth collection, usually users:
- Open your app's dashboard (
/_/) and go to Collections. - Open the
userscollection's settings (the gear icon) and its Options tab. - Under OAuth2, turn it on.
- Click Add provider and pick Google.

There's no site URL to set for this. The auth plugin builds the redirect URL from the address the request came in
on: the scheme and host the browser used, plus /auth/oauth/confirm. So the redirect URI in Google must match that
address exactly. http://localhost:8090 and http://127.0.0.1:8090 are different to Google, so add each one you
use, plus your live domain (https://example.com/auth/oauth/confirm). Behind a proxy, the host is the one the
browser asked for, and HTTPS is detected from X-Forwarded-Proto.
Provider Configuration
- Enter your Client ID and Client Secret from the downloaded credentials
- Configure any additional auth options as needed
- Save your changes

Customizing the Return URL
By default, the auth plugin uses /auth/oauth/confirm as the OAuth2 return path. Change it when you start the flow:
ctx.session.requestOAuth2('google', { redirectPath: '/custom/oauth/callback' })
Remember to add the new redirect URL to your Google OAuth2 configuration too.
Implementation Guide
1. Turn on the auth plugin
// pb_pages/+config.ts
module.exports = {
plugins: ['auth'],
}
2. Login form
<form method="post" action="/auth/oauth">
<input type="hidden" name="provider" value="google" />
<button type="submit">Sign in with Google</button>
</form>
3. Start the flow
// pb_pages/auth/oauth.ts
module.exports = {
post(ctx) {
ctx.session.requestOAuth2(ctx.req.body().provider)
},
}
requestOAuth2 stores its state in a cookie and redirects the user to Google's consent screen.
4. Callback
// pb_pages/auth/oauth/confirm/index.ts
module.exports = {
load(ctx) {
try {
ctx.session.signInWithOAuth2(ctx.req.query('state'), ctx.req.query('code'))
} catch (e) {
return { error: e.message }
}
return ctx.res.redirect('/')
},
}
<!-- pb_pages/auth/oauth/confirm/index.ets -->
<mark><%= data.error %></mark>
5. Migration
Set up Google OAuth2 in a PocketBase migration:
migrate(
(app) => {
const users = app.findCollectionByNameOrId('users')
users.oauth2.enabled = true
users.oauth2.providers = [
{
name: 'google',
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
},
]
app.save(users)
},
(app) => {
const users = app.findCollectionByNameOrId('users')
users.oauth2.enabled = false
users.oauth2.providers = []
app.save(users)
}
)
6. Protected routes
// pb_pages/account/+middleware.ts
module.exports = (ctx, next) => {
if (!ctx.auth) return ctx.res.redirect('/login')
return next()
}
7. Auth state in the UI
<% if (ctx.auth) { %>
<div>Welcome, <%= ctx.auth.email() %></div>
<form method="post" action="/logout"><button>Log out</button></form>
<% } else { %>
<a href="/login">Log in</a>
<% } %>
8. Logout
// pb_pages/logout.ts
module.exports = {
post(ctx) {
ctx.session.signOut()
return ctx.res.redirect('/')
},
}
See Authentication and the auth plugin.
Troubleshooting
Common issues and solutions:
- Invalid redirect URI: Ensure your redirect URI exactly matches what's configured in Google Cloud Console
- Consent screen not showing: Make sure you've properly configured the OAuth consent screen
- Authentication failed: Verify your client ID and secret are correctly copied to the dashboard
Security Considerations
- Always use HTTPS in production
- Keep your client secret secure
- Regularly rotate credentials
- Review Google OAuth2 access regularly in your Google Cloud Console
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.