Authentication Overview
PocketPages provides seamless integration with PocketBase's authentication system, supporting both header-based and cookie-based authentication methods. Each authentication method is designed for specific use cases and can be used individually or in combination.
Authentication Flow
PocketPages automatically checks for authentication tokens in this order:
Authorization
header (highest priority)pb_auth
cookie (fallback)
When a token is found, PocketPages automatically populates the request.auth
context with a core.Record
instance containing the authenticated user's information.
The complete authentication flow is:
- User initiates authentication
- Authentication method validates credentials
- PocketBase generates JWT token
- PocketPages sets
pb_auth
cookie - Subsequent requests include authentication
Common Features
All authentication methods in PocketPages share these characteristics:
- Cookie-Based: Authentication state is maintained via the
pb_auth
cookie - Automatic Headers: Auth tokens are automatically included in requests
- Context Access: Authentication state is available via
request.auth
- Type Safety: All methods return strongly typed
AuthData
Available Methods
Password Authentication
Traditional email/password authentication:
const authData = api.signInWithPassword(email, password)
Best for:
- Traditional web applications
- Users who prefer email/password login
- Applications requiring email verification
Learn more about Password Authentication
OAuth2 Authentication
Social login with providers like Google, GitHub, etc:
const authUrl = api.requestOAuth2Login('google')
Best for:
- Social login integration
- Reducing registration friction
- Accessing provider APIs
Learn more about OAuth2 Authentication
One-Time Password (OTP)
Passwordless email authentication:
const otpData = api.requestOTP(email)
Best for:
- Passwordless authentication
- Temporary access
- Enhanced security requirements
Learn more about OTP Authentication
Anonymous Authentication
Guest access with persistent identity:
const authData = api.signInAnonymously()
Best for:
- Try-before-you-register flows
- Guest checkout
- Temporary user sessions
Learn more about Anonymous Authentication
Token Authentication
Manual token management:
api.signInWithToken(token)
Best for:
- Custom authentication flows
- External authentication systems
- API integrations
Learn more about Token Authentication
Working with Auth Records
The request.auth
context provides a core.Record
instance with methods to access user data:
<% if (request.auth) { %>
<!-- Using Record methods -->
<p>Welcome, <%= request.auth.get('email') %></p>
<p>Username: <%= request.auth.get('username') %></p>
<!-- Check verification status -->
<% if (request.auth.verified()) { %>
<p>✓ Verified account</p>
<% } %>
<!-- Access custom fields -->
<p>Role: <%= request.auth.get('role') %></p>
<% } else { %>
<p>Please log in</p>
<% } %>
Common core.Record
methods for auth:
get(field)
- Get a field valueemail()
- Get the email addressverified()
- Check if email is verifiedisSuperuser()
- Check if user is admincollection()
- Get the auth collection
Client-Side Integration
When using the PocketBase JavaScript SDK alongside PocketPages, you'll need to synchronize the SDK's auth state with the cookie-based MPA approach:
// Initialize PocketBase client
const pb = new PocketBase('http://127.0.0.1:8090')
// Load auth state from cookies
pb.authStore.loadFromCookie(document.cookie)
// Optional: Keep auth state in sync when cookie changes
pb.authStore.onChange(() => {
document.cookie = pb.authStore.exportToCookie()
})
This ensures the SDK's auth store stays in sync with the server-side authentication state managed by PocketPages.
Signing Out
All authentication methods can use the common sign-out function:
api.signOut()
This will:
- Clear the
pb_auth
cookie - Remove the auth token
- Reset the authentication state
Choosing an Auth Method
Consider these factors when choosing authentication methods:
User Experience
- Password: Familiar but requires remembering credentials
- OAuth2: Quick but requires third-party account
- OTP: Simple but requires email access
- Anonymous: Frictionless but limited
Security Requirements
- Password: Strong with proper policies
- OAuth2: Delegated to trusted providers
- OTP: Time-limited, email-verified
- Anonymous: Limited access scope
Implementation Complexity
- Password: Moderate (email verification, password reset)
- OAuth2: Higher (provider setup, callback handling)
- OTP: Lower (email delivery only)
- Anonymous: Lowest (single method call)
Maintenance
- Password: Password resets, security policies
- OAuth2: Provider API changes, token refresh
- OTP: Email deliverability
- Anonymous: Data cleanup policies
Security Best Practices
- Use HTTPS in production
- Implement rate limiting
- Enable email verification
- Set appropriate token expiration
- Monitor authentication events
Auth Starter Kit
PocketPages provides a complete authentication starter kit that demonstrates all these authentication methods working together:
npx tiged benallfree/pocketpages/starters/auth .
The starter kit includes:
- Complete authentication UI
- All authentication methods implemented
- Email verification flow
- Account management
- Password reset flow
- Middleware protection
- Layout with auth status