templates2.Booking.Nuxt/server/middleware/guard.ts
w4c-templates 3491e2d39c
Some checks failed
CI / quality (push) Has been cancelled
chore(template): publish Booking.Nuxt
2026-10-11 23:27:22 +03:00

13 lines
568 B
TypeScript

// Server middleware: coarse auth gate for protected pages, before rendering.
// Keep the cookie name in sync with server/utils/auth.ts.
const SESSION_COOKIE = 'w4c_session';
const PROTECTED = ['/dashboard', '/account', '/admin'];
export default defineEventHandler((event) => {
const path = getRequestURL(event).pathname;
const isProtected = PROTECTED.some((p) => path === p || path.startsWith(`${p}/`));
if (!isProtected) return;
if (getCookie(event, SESSION_COOKIE)) return;
return sendRedirect(event, `/login?next=${encodeURIComponent(path)}`, 302);
});