In short: PocketBase gives you a ready-to-use users collection. With the official JavaScript SDK you call authWithPassword() or authWithOAuth2(), read the session from pb.authStore, and protect your data with API rules such as @request.auth.id != "".
Authentication is usually the first thing a new backend needs and the easiest to get wrong. This tutorial walks through email and password sign-up, login, logout and Google sign-in, then shows how to lock down your data on the server side. It assumes your backend is already online, for example after our PocketBase production deployment guide.
Prerequisites
- A running PocketBase instance (v0.23 or later) and its public URL
- Node.js 18 or later with npm, or a project that uses ES modules in the browser
- Basic JavaScript knowledge (
asyncandawait) - For OAuth2: a Google Cloud project where you can create OAuth credentials
Step 1: install and initialize the SDK
bashnpm install pocketbase
jsimport PocketBase from 'pocketbase';
export const pb = new PocketBase('https://pb.example.com');
In the browser, the SDK stores the session in localStorage through its default authStore, so users stay signed in after a refresh.
Step 2: sign up with email and password
PocketBase includes a users auth collection. Create a record in it, then ask for an email verification:
jsawait pb.collection('users').create({
email: '[email protected]',
password: 'a-long-unique-password',
passwordConfirm: 'a-long-unique-password',
});
await pb.collection('users').requestVerification('[email protected]');
Verification emails need a working SMTP server in the Dashboard settings. Without one, messages may fail or land in spam.
Step 3: log in
jsconst authData = await pb
.collection('users')
.authWithPassword('[email protected]', 'a-long-unique-password');
console.log(pb.authStore.isValid); // true
console.log(pb.authStore.record.id); // the signed-in user's id
Older SDK versions expose the user as pb.authStore.model. In current versions, use pb.authStore.record.
Step 4: react to session changes and log out
jspb.authStore.onChange((token, record) => {
console.log('Auth changed:', record?.email ?? 'signed out');
});
// Refresh the token when the app starts
if (pb.authStore.isValid) {
await pb.collection('users').authRefresh();
}
// Log out
pb.authStore.clear();
Step 5: add Google sign-in with OAuth2
- In Google Cloud, create OAuth 2.0 credentials of type Web application.
- Add this redirect URL:
https://pb.example.com/api/oauth2-redirect. - In the PocketBase Dashboard, open the
userscollection settings, go to the OAuth2 options, enable Google, and paste the client ID and client secret. - Call the SDK from your front end:
jsconst authData = await pb
.collection('users')
.authWithOAuth2({ provider: 'google' });
console.log(authData.record.email);
The SDK opens a popup, completes the flow and stores the session just like a password login. Allow popups for your site, or use the redirect-based flow in the SDK documentation for strict mobile browsers.
Step 6: protect your data with API rules
Authentication tells PocketBase who the user is. API rules decide what they can do. Without rules, a collection is not accessible to regular users at all, so open it up deliberately. In the Dashboard, edit the collection’s API rules, for example for a notes collection with a user relation field:
- List and view rule:
user = @request.auth.id - Create rule:
@request.auth.id != "" - Update and delete rule:
user = @request.auth.id
With these rules, signed-in users can only read and change their own notes, whatever the front end does.
Security checklist
- Never ship superuser credentials in front-end code
- Use HTTPS everywhere, including the OAuth2 redirect URL
- Configure SMTP and enable email verification
- Keep rate limiting enabled on login routes
- Write API rules for every collection, then test them while signed out and as another user
Common errors
- 400 on login: wrong email or password, or the email is not verified when your rules require it.
- Popup blocked during OAuth2: trigger
authWithOAuth2()from a direct click handler. - Redirect URI mismatch: the URL in Google Cloud must match PocketBase’s redirect URL exactly, including
httpsand the path. - 404 or 403 when reading records: the collection rule is empty or too strict for the signed-in user.
FAQ
Can I use PocketBase authentication with React, Vue or Svelte?
Yes. The SDK is framework-agnostic. Create one pb client and expose pb.authStore through your state management or a small hook or composable.
Where does PocketBase store the session?
In the browser, the default auth store uses localStorage. For server-side rendering, load and export the session through cookies, as described in the SDK documentation.
Should I use PocketBase auth or an identity provider like Keycloak?
PocketBase authentication is the fastest option for apps with their own user base. If you need enterprise single sign-on across several applications, a dedicated identity provider fits better. See our guide to authenticating a NuxtJS application with Keycloak.
Next steps
Read the official JavaScript SDK documentation for realtime subscriptions and file uploads, and see how the REST layer works in PocketBase: the easy way to create your secure REST API.