import cookie from "cookie"; import db from "../db"; import type { Middleware } from "waku/config"; const cookieMiddleware: Middleware = () => { return async (ctx, next) => { // Parse incoming cookies const cookies = cookie.parse(ctx.req.headers.cookie || ""); const coki = cookies.sorlang; // If cookie exists, fetch user data and set in context if (coki) { const userRow = db.fetchCookie(coki); if (userRow) ctx.data.user = { id: userRow.id, name: userRow.name }; // console.log("User authenticated:", userRow.name); } // Uncomment to enable redirection for unauthenticated users /* if (!ctx.data.user && ctx.req.url.pathname !== "/login") { ctx.res.status = 302; ctx.res.headers = { Location: "/login", }; return; } */ await next(); // Cookie setting is now handled by setCookieMiddleware }; }; export default cookieMiddleware;