domaino/app-admin/src/hooks.server.ts
agent 1518f1bbbe Fix admin login/logout over HTTP and stale sessions
- Auth cookie was always exported with the SDK default `Secure` flag, so
  browsers dropped it over plain HTTP (CADDY_SCHEME=http) and login
  looped back to /login. Set `secure` from the real request scheme.
- Logout never ran: the hook redirected every authenticated request to
  /login (including POST /login?/logout) back to /pages. Only redirect GET.
- Refresh the token per request so revoked tokens log the user out instead
  of surfacing as 500s, and sessions slide instead of expiring mid-edit.
- Use `_superusers` instead of the deprecated `pb.admins` alias; keep the
  entered email on a failed login.
- docker-compose: fully-qualified image names (podman rejects short names).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
2026-10-06 10:35:15 +00:00

45 lines
1.6 KiB
TypeScript

import { redirect, type Handle } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';
import { createPocketBase } from '$lib/pb';
export const handle: Handle = async ({ event, resolve }) => {
event.locals.pb = createPocketBase(
event.request.headers.get('cookie') ?? '',
env.POCKETBASE_INTERNAL_URL
);
// `isValid` only checks the token's expiry locally. Refresh it against
// PocketBase so a revoked token (deleted user, changed password) logs the
// user out instead of failing later as a 500, and so active sessions slide.
if (event.locals.pb.authStore.isValid) {
const collection = event.locals.pb.authStore.record?.collectionName || '_superusers';
try {
await event.locals.pb.collection(collection).authRefresh();
} catch {
event.locals.pb.authStore.clear();
}
}
event.locals.user = event.locals.pb.authStore.record;
const path = event.url.pathname;
const isLoginRoute = path === '/login';
const isAuthed = event.locals.pb.authStore.isValid;
if (!isLoginRoute && !isAuthed) {
throw redirect(303, '/login');
}
if (isLoginRoute && isAuthed && event.request.method === 'GET') {
throw redirect(303, '/pages');
}
const response = await resolve(event);
// The SDK defaults to `Secure`, which browsers drop over plain HTTP — that
// would make login impossible with CADDY_SCHEME=http. Match the real scheme
// (event.url comes from ORIGIN behind the proxy).
response.headers.append(
'set-cookie',
event.locals.pb.authStore.exportToCookie({ sameSite: 'lax', secure: event.url.protocol === 'https:' }, 'pb_auth')
);
return response;
};