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

  1. Go to Google Cloud Console
  2. Either select an existing project or create a new one
  3. If creating new, provide a project name when prompted

Creating a new Google Cloud project Naming the new project Selecting the project

2. Configure OAuth Credentials

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

Navigating to credentials Creating OAuth credentials

Configure Application Type

  1. Select "Web application" as the application type
  2. 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)

Selecting web application Configuring URIs

If you haven't set up a consent screen before, you'll need to configure it:

  1. Choose External/Internal user type
  2. Fill in the required application information

Consent screen setup Consent screen details

Download Credentials

  1. After creating the OAuth client, download the JSON credentials file
  2. Store this file securely - it contains sensitive information

Downloading credentials Credentials JSON format

3. Configure OAuth2 in the dashboard

OAuth2 providers belong to an auth collection, usually users:

  1. Open your app's dashboard (/_/) and go to Collections.
  2. Open the users collection's settings (the gear icon) and its Options tab.
  3. Under OAuth2, turn it on.
  4. Click Add provider and pick Google.

OAuth2 settings for the users collection OAuth2 providers section Adding new provider Selecting Google provider

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

  1. Enter your Client ID and Client Secret from the downloaded credentials
  2. Configure any additional auth options as needed
  3. Save your changes

Configuring Google provider Saving 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