Skip to content

AuthService

pb.auth is an instance of AuthService. It handles all authentication, session tokens, JWT introspection, and admin management.

{
access_token: string;
id_token?: string;
refresh_token?: string;
session_id?: string;
token_type: string; // 'Bearer'
expires_in: number; // seconds
}
{
sub: string; // User's unique ID
email?: string;
email_verified?: boolean;
given_name?: string;
family_name?: string;
picture?: string; // Avatar URL
roles?: string[];
[key: string]: any; // Custom claims
}
{
secret: string;
qr_code_url?: string; // Scan with authenticator app
recovery_codes?: string[]; // Store securely — shown only once
}

signUpWithPassword(email, password, options?)

Section titled “signUpWithPassword(email, password, options?)”

Register a new user. On success, the user is automatically signed in and tokens are stored.

const result = await pb.auth.signUpWithPassword(
'jane@example.com',
'SecurePassword123!',
{
given_name: 'Jane',
family_name: 'Doe',
user_metadata: { plan: 'free' }, // Stored in user profile
}
);
// Returns: AuthTokens & { user_id: string }

signInWithPassword(email, password, mfaCode?)

Section titled “signInWithPassword(email, password, mfaCode?)”

Sign in with email and password. If MFA is enabled, first call returns { mfa_required: true }.

const session = await pb.auth.signInWithPassword('jane@example.com', 'Password123!');
// MFA handling:
if ('mfa_required' in session) {
const code = await promptUser();
await pb.auth.signInWithPassword('jane@example.com', 'Password123!', code);
}

Returns: AuthTokens | { mfa_required: true }

Revoke the current refresh token and clear the local session.

await pb.auth.signOut();

getAuthorizationUrl(state, codeChallenge, scopes?)

Section titled “getAuthorizationUrl(state, codeChallenge, scopes?)”

Build the authorization URL for redirect-based login (PKCE).

const url = pb.auth.getAuthorizationUrl(state, codeChallenge, 'openid profile email');
window.location.href = url;

Exchange an authorization code for tokens (PKCE final step).

const tokens = await pb.auth.exchangeCode(codeFromUrl, storedCodeVerifier);

Fetch fresh user info from the IDP. Updates the cached user.

const user = await pb.auth.getUser();
// Returns: AuthUser

Return the locally cached user (no network call). null if not signed in.

const user = pb.auth.getCachedUser();
// Returns: AuthUser | null

Returns a valid access token, refreshing it first if it’s about to expire. Returns null if not authenticated.

const token = await pb.auth.getValidAccessToken();

Returns the current raw tokens (may be expired).

const tokens = pb.auth.getTokens();
// Returns: AuthTokens | null

Manually set tokens (e.g., after an OAuth callback).

pb.auth.setSession({ access_token: '...', refresh_token: '...', token_type: 'Bearer', expires_in: 3600 });

Manually trigger a token refresh.

const newTokens = await pb.auth.refreshSession();

Check if the user is currently signed in (token exists in memory).

if (pb.auth.isAuthenticated()) { ... }

Request a password reset email. No error thrown if email doesn’t exist (security best practice).

await pb.auth.resetPassword('user@example.com');

Complete a password reset using the token from the email link.

await pb.auth.confirmResetPassword(tokenFromUrl, 'NewPassword456!');

updatePassword(currentPassword, newPassword)

Section titled “updatePassword(currentPassword, newPassword)”

Change password for the currently signed-in user. Requires the current password.

await pb.auth.updatePassword('CurrentPass123!', 'NewPass456!');

Send (or resend) a verification email to the current user.

await pb.auth.requestEmailVerification();

Confirm email verification using the token from the verification email.

await pb.auth.confirmEmailVerification(tokenFromUrl);

Enroll the current user in TOTP-based MFA. Returns a QR code URL for authenticator apps and recovery codes.

const { qr_code_url, recovery_codes } = await pb.auth.mfa.enroll();
// Display qr_code_url to user for scanning
// Store recovery_codes securely — shown only once!

Activate MFA by verifying the first TOTP code.

await pb.auth.mfa.verify('123456');

Disable MFA for the current user.

await pb.auth.mfa.disable();

All of these read from the cached JWT without making any network requests.

const roles = pb.auth.getRoles(); // string[]
const perms = pb.auth.getPermissions(); // string[]
pb.auth.hasRole('admin') // boolean

Returns true if the user has at least one of the specified roles.

pb.auth.hasAnyRole('admin', 'moderator')
pb.auth.hasPermission('content.publish')

Returns true only if the user has all specified permissions.

pb.auth.hasAllPermissions('content.read', 'content.write')

Returns true if the user has at least one of the specified permissions.

pb.auth.hasAnyPermission('content.read', 'content.admin')

All management methods require the caller to have an admin role in their JWT.

const users = await pb.auth.listUsers();
const user = await pb.auth.getUserById('user-uuid');
await pb.auth.updateUser(userId, {
email_verified: true,
status: 'active', // 'active' | 'disabled'
given_name: 'Jane',
family_name: 'Doe',
roles: ['admin', 'editor'],
});

const roles = await pb.auth.listAllRoles();
await pb.auth.createRole('editor', 'Can create and edit content');
await pb.auth.deleteRole('editor');

Replaces the user’s current roles with the given list.

await pb.auth.assignRoles(userId, ['admin', 'editor']);

const perms = await pb.auth.listAllPermissions();
await pb.auth.createPermission('content.publish', 'Publish articles to production');
await pb.auth.deletePermission('content.publish');

Replace the permission set for a role.

await pb.auth.setRolePermissions('editor', ['content.read', 'content.write']);
const perms = await pb.auth.getRolePermissions('editor'); // string[]

Set user-specific permission overrides (bypasses role-based logic).

await pb.auth.setUserPermissions(userId, ['reports.view'], ['billing.access']);
const { allow_permissions, deny_permissions } = await pb.auth.getUserPermissions(userId);