13 lines
568 B
TypeScript
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);
|
|
});
|