Authentication
Pages keeps PocketBase auth in a cookie with the built-in auth plugin. Turn it on:
// pb_pages/+config.ts
module.exports = {
plugins: ['auth'],
}
How a request is authenticated
- If the request has PocketBase's own
Authorizationheader,ctx.authis that record. - Otherwise, the
authplugin reads thepb_authcookie, validates the token and setsctx.auth(or clears a bad cookie). - With neither,
ctx.authisnull.
Every ctx.session sign-in method sets the cookie, sets ctx.auth for the rest of the request, and returns { token, record }. ctx.session.signOut() clears it.
Protecting pages
// pb_pages/account/+middleware.ts
module.exports = (ctx, next) => {
if (!ctx.auth) return ctx.res.redirect('/login')
return next()
}
<% if (ctx.auth) { %>
<p>Welcome, <%= ctx.auth.email() %></p>
<form method="post" action="/logout"><button>Log out</button></form>
<% } else { %>
<a href="/login">Log in</a>
<% } %>
Passwords
// pb_pages/login/index.ts
module.exports = {
post(ctx) {
const { email, password } = ctx.req.body()
try {
ctx.session.signInWithPassword(email, password)
} catch (e) {
return { error: e.message, email }
}
return ctx.res.redirect('/')
},
}
<!-- pb_pages/login/index.ets -->
<form method="post">
<label>Email</label>
<input name="email" type="email" value="<%= data.email %>" />
<label>Password</label>
<input name="password" type="password" />
<button type="submit">Log in</button>
</form>
<% if (data.error) { %><mark><%= data.error %></mark><% } %>
Registration
// pb_pages/register.ts
module.exports = {
post(ctx) {
const { email, password, name } = ctx.req.body()
try {
ctx.session.register(email, password, { data: { name } })
} catch (e) {
return { error: `Error registering: ${e.message}` }
}
return ctx.res.redirect('/', { flash: 'Welcome!' })
},
}
One-time passwords
A two-step form: request a code, then sign in with it. Turn on OTP for the collection in the dashboard and set up mail (Settings > Mail settings).
// pb_pages/login/otp/index.ts
module.exports = {
post(ctx) {
const { step, email, otpId, code } = ctx.req.body()
try {
if (step === 'request') {
return { otpId: ctx.session.requestOTP(email), email }
}
ctx.session.signInWithOTP(otpId, code)
return ctx.res.redirect('/', { flash: 'Signed in' })
} catch (e) {
return { error: e.message, otpId, email }
}
},
}
<!-- pb_pages/login/otp/index.ets -->
<% if (data.error) { %><mark><%= data.error %></mark><% } %>
<% if (!data.otpId) { %>
<form method="post">
<input type="hidden" name="step" value="request" />
<input name="email" type="email" required />
<button type="submit">Send code</button>
</form>
<% } else { %>
<p>Check your email for the code.</p>
<form method="post">
<input type="hidden" name="step" value="confirm" />
<input type="hidden" name="otpId" value="<%= data.otpId %>" />
<input name="code" required />
<button type="submit">Sign in</button>
</form>
<% } %>
OAuth2
Configure the provider in the dashboard (Collections > users > Options > OAuth2). Then start the flow from a form:
<form method="post" action="/auth/oauth">
<input type="hidden" name="provider" value="google" />
<button type="submit">Sign in with Google</button>
</form>
// 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 to the provider. The provider sends the user back to /auth/oauth/confirm (change it with { redirectPath }):
// 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>
See the Google OAuth2 speedrun for setting up Google.
Anonymous sign-in
// pb_pages/try.ts
module.exports = {
post(ctx) {
ctx.session.signInAnonymously()
return ctx.res.redirect('/dashboard')
},
}
Your own auth flow
If you authenticate a record yourself, set the session cookie with ctx.session.signIn(record).
Signing out
// pb_pages/logout.ts
module.exports = {
post(ctx) {
ctx.session.signOut()
return ctx.res.redirect('/')
},
}
Security
- Serve over HTTPS in production.
- Turn on PocketBase's rate limiting.
- Turn on email verification for your auth collection.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.