import { getCookie, getSignedCookie, setCookie, setSignedCookie, deleteCookie, } from "hono/cookie"; import cookie from "cookie"; // console.log("db module path:", "@/lib/db"); // console.log( // "globalThis.__WAKU_MIDDLEWARE_CONTEXT_STORAGE__:", // globalThis.__WAKU_MIDDLEWARE_CONTEXT_STORAGE__, // ); import db from "../db"; import type { Middleware } from "waku/config"; // XXX we would probably like to extend config. const cookieMiddleware: Middleware = () => { console.log("cookieMiddleware executed"); return async (ctx, next) => { const cookies = cookie.parse(ctx.req.headers.cookie || ""); console.log({ cookies }); const coki = cookies.sorlang; if (!coki) { if (ctx.req.url.pathname === "/login") return await next(); ctx.res.status = 301; ctx.res.headers = { Location: "/login", }; } if (coki) { const userRow = db.fetchCookie(coki); if (userRow) ctx.data.user = { id: userRow.id, name: userRow.name }; else { if (ctx.req.url.pathname === "/login") return await next(); ctx.res.status = 301; ctx.res.headers = { Location: "/login", }; } } await next(); }; }; export default cookieMiddleware;