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

13 lines
556 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', '/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);
});