domaino/app-admin/src/routes/login/+page.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

28 lines
884 B
TypeScript

import { fail, redirect } from '@sveltejs/kit';
import type { Actions } from './$types';
export const actions: Actions = {
login: async ({ request, locals }) => {
const data = await request.formData();
const email = String(data.get('email') || '');
const password = String(data.get('password') || '');
try {
try {
await locals.pb.collection('_superusers').authWithPassword(email, password);
} catch {
await locals.pb.collection('users').authWithPassword(email, password);
}
} catch {
// One line per failed attempt; the full SDK error object is noise here.
console.warn('Admin login failed for', email);
return fail(400, { email, message: 'Invalid email or password.' });
}
throw redirect(303, '/pages');
},
logout: async ({ locals }) => {
locals.pb.authStore.clear();
throw redirect(303, '/login');
}
};