In this guide, we add a "Continue with Google" button to a React app. A user clicks it and signs in with their Google account. PocketBase then creates a user with their name, email and profile photo.
You don't write any backend code. And your Google client secret never goes into your React code.
We use three tools:
- PocketBase: a backend in one file. It has a database, user accounts and an admin dashboard. It can also sign users in with Google.
- React + Vite: Vite creates a React project and runs it on your computer.
- pbc: the command-line tool (CLI) for PocketBase Cloud. We use it to create the project and to give PocketBase the Google keys.
Words you will see
| Word | Meaning |
|---|---|
| OAuth2 | A standard way to sign in with another service, like Google. |
| Provider | The service that signs the user in. Here, it is Google. |
| Client ID | A public ID for your app. Google gives it to you. |
| Client secret | A private password for your app. Only the server should know it. |
| Redirect URI | The address Google sends the user back to after they sign in. |
users collection |
The table in PocketBase where user accounts are saved. |
| Superuser | The admin account of a PocketBase instance. |
What you need
- A Google account
- Node.js (for React and Vite)
- A terminal and a code editor
How the login works
- The user clicks "Continue with Google". A popup opens with Google's sign-in page.
- The user signs in. Google sends them to PocketBase at
/api/oauth2-redirect, with a one-time code. - PocketBase exchanges the code with Google. This step needs the client secret. PocketBase gets the user's name, email and photo, and creates (or updates) the user in the
userscollection. - PocketBase sends a login token back to the React app. The popup closes, and the user is signed in.
The client secret is only used in step 3, on the server. That's why it stays safe.
These are the files we will have at the end:
Step 1: Create a PocketBase project
Install the pbc CLI:
curl -fsSL https://raw.githubusercontent.com/pocketbasecloud/cli/main/scripts/install.sh | sh
Create a folder, set up PocketBase and start it:
mkdir react-google
cd react-google
pbc local init
./pocketbase serve
pbc local init downloads PocketBase for your computer and saves its version in pbc.json. ./pocketbase serve starts the server at http://127.0.0.1:8090. Keep this terminal open.
Open a second terminal in the same folder. Create a superuser, then log in with pbc:
./pocketbase superuser upsert you@example.com 'a-long-password'
pbc admin login --url http://127.0.0.1:8090
The second command asks for your email and password.
Step 2: Get a Google Client ID
Here is an overview of what we do in the Google Cloud console:
The screenshots below are from the real Google Cloud console. We blurred the IDs.
2.1 Create a project
Open the Google Cloud console and create a new project. Any name works.
2.2 Set the audience
Go to Google Auth Platform → Audience. Choose the user type External. Then, under Test users, click Add users and add your own Gmail address.
Why? A new app starts in Testing mode. In this mode, only test users can sign in.
The first time you open Google Auth Platform, Google asks for an app name and a support email. Fill them in.
2.3 Create a client
Go to Clients and click Create client.
For Application type, choose Web application:
Then give the client a name, for example PocketBase local. Only you can see this name.
2.4 Add the redirect URI
Under Authorized redirect URIs, click Add URI and paste this address. Then click Create.
http://127.0.0.1:8090/api/oauth2-redirect
- The URI must match exactly. For Google,
localhostand127.0.0.1are different addresses. - You can leave Authorized JavaScript origins empty. Google sends the user to PocketBase, not to your React app.
- Google says a change can take from 5 minutes to a few hours to work.
2.5 Copy the Client ID and Client secret
Right after you click Create, Google shows the Client ID and the Client secret. Copy both now.
The secret is shown only once. Later, the client page only shows a hidden version of it. If you lose the secret, you have to add a new one.
Step 3: Connect Google to PocketBase
Create a file named oauth2.json in your project folder. Paste your Client ID and Client secret into it:
{
"enabled": true,
"providers": [
{
"name": "google",
"clientId": "YOUR_CLIENT_ID.apps.googleusercontent.com",
"clientSecret": "YOUR_CLIENT_SECRET"
}
],
"mappedFields": { "name": "name", "avatarURL": "avatar" }
}
-
enabled: turns on OAuth2 login for theuserscollection. -
providers: the list of sign-in services. We only have Google. -
mappedFields: copies the Google name into thenamefield. It also downloads the Google profile photo into theavatarfield.
Now send this setting to PocketBase, and keep the file out of git:
pbc admin auth users config --set "oauth2=$(cat oauth2.json)"
echo oauth2.json >> .gitignore
Good to know:
-
--setreplaces the wholeoauth2setting. If you add another provider later, put all providers in the file. - PocketBase never shows the secret again. When you read the setting back, there is no
clientSecretin it. - PocketBase also writes a migration file for this change. It only says "OAuth2 is enabled". Your keys are not in it.
You can check the result in the dashboard. Open http://127.0.0.1:8090/_/, click users, click the gear icon, then open Options → OAuth2:
Step 4: Create the React app
In the project folder, create a React app with Vite:
npm create vite@latest web -- --template react
cd web
npm install
npm install pocketbase water.css
npm run dev
-
pocketbaseis the official PocketBase JavaScript SDK. -
water.cssgives us simple styles, so we don't need to write CSS. -
npm run devstarts the app athttp://localhost:5173.
Step 5: Add the "Continue with Google" button
Replace three files in web/src/.
src/pb.js creates one PocketBase client for the whole app:
import PocketBase from "pocketbase"
export const pb = new PocketBase("http://127.0.0.1:8090")
The SDK saves the login in the browser (in localStorage). So the user stays signed in after a page reload.
src/main.jsx loads the styles and shows the app:
import { StrictMode } from "react"
import { createRoot } from "react-dom/client"
import "water.css"
import App from "./App.jsx"
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>,
)
src/App.jsx has the button and the profile:
import { useEffect, useState } from "react"
import { pb } from "./pb"
export default function App() {
const [user, setUser] = useState(pb.authStore.record)
const [error, setError] = useState("")
useEffect(() => pb.authStore.onChange((_, record) => setUser(record)), [])
async function login(provider) {
try {
await pb.collection("users").authWithOAuth2({ provider })
} catch (err) {
setError(err.message)
}
}
if (!user) {
return (
<main>
<h1>Welcome</h1>
<button onClick={() => login("google")}>Continue with Google</button>
{error && <p style={{ color: "crimson" }}>{error}</p>}
</main>
)
}
const avatar = user.avatar && pb.files.getURL(user, user.avatar)
return (
<main>
{avatar && <img src={avatar} width="72" style={{ borderRadius: "50%" }} />}
<h1>Hi, {user.name || user.email}</h1>
<p>Signed in as <b>{user.email}</b></p>
<button onClick={() => pb.authStore.clear()}>Log out</button>
</main>
)
}
What this code does:
-
useState(pb.authStore.record): starts with the current user. It isnullwhen nobody is signed in. -
pb.authStore.onChange(...): runs every time the user signs in or out, and updatesuser. It returns a function that removes the listener. We return that function fromuseEffect, so React can clean up. -
authWithOAuth2({ provider: "google" }): does the whole login. It opens the popup, waits for PocketBase and saves the token. - If something goes wrong, for example the user closes the popup, we show the error message.
-
pb.files.getURL(user, user.avatar): makes a URL for the photo that PocketBase saved. -
pb.authStore.clear(): logs the user out. It removes the token from the browser.
Open http://localhost:5173:
What happens when you click
We captured the first requests from the running app:
Look at redirect_uri. It is exactly the URI we added in Google in Step 2.4. If the two don't match, Google shows a redirect_uri_mismatch error.
Now sign in with your test user. The popup closes, and the app shows your name, email and Google photo. In the dashboard, the users collection now has a new record for you.
Common problems
| Problem | What to check |
|---|---|
redirect_uri_mismatch |
The redirect URI in Google must be exactly http://127.0.0.1:8090/api/oauth2-redirect. |
| "Access blocked" for some accounts | The app is in Testing mode. Add the account under Audience → Test users. |
| The popup does not open | Allow popups for localhost in your browser. |
| An error after you sign in | Check the Client ID and secret in oauth2.json, then run the pbc admin auth command again. |
Step 6: Go live
When the app works on your computer, you can put it online with PocketBase Cloud.
1. Deploy PocketBase. In the project folder:
pbc login
pbc pocketbase deploy --new react-google
This creates a new instance. It also prints a superuser password only once, so save it.
2. Add a second redirect URI in Google. Use the address of your new instance:
https://YOUR-INSTANCE-ADDRESS/api/oauth2-redirect
3. Give the Google keys to the cloud instance. Migrations don't carry the keys, so send them again:
pbc admin login --url https://YOUR-INSTANCE-ADDRESS
pbc admin auth users config --set "oauth2=$(cat oauth2.json)"
4. Deploy the React app. In web/src/pb.js, change the URL to https://YOUR-INSTANCE-ADDRESS. Then:
cd web
pbc website deploy --new react-google-web
5. Let everyone sign in. Right now only test users can sign in. When you are ready, go to Google Auth Platform → Audience and click Publish app. Google may ask you to verify your app.
What's next
Some ideas to try next:
- Add more providers, like GitHub or Microsoft. Put all of them in
oauth2.json. - Save data for each user, and protect it with API rules like
@request.auth.id != "". - Show a loading state while the popup is open.
Useful links:














Top comments (0)