// Для HTML-страниц гарантирует session_id: читает cookie или заводит новый
// (httpOnly, 30 дней) и кладёт его в event.context.sessionId.
import { randomUUID } from 'crypto';
import { getCookie, setCookie } from 'h3';
export default defineEventHandler((event) => {
const req = event.node.req;
const accept = req.headers.accept || '';
// Только HTML-страницы
if (!accept.includes('text/html')) return;
let sessionId = getCookie(event, 'session_id');
if (!sessionId) {
sessionId = randomUUID();
setCookie(event, 'session_id', sessionId, {
httpOnly: true,
path: '/',
maxAge: 60 * 60 * 24 * 30,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax'
});
}
event.context.sessionId = sessionId;
});