From 490388360a0852bcf8ee054e96fa90e166df5792 Mon Sep 17 00:00:00 2001 From: polwex Date: Thu, 29 May 2025 14:52:38 +0700 Subject: fucker actually solved the cookies, love ya man --- src/lib/server/cookie.ts | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'src/lib/server/cookie.ts') diff --git a/src/lib/server/cookie.ts b/src/lib/server/cookie.ts index 9a7e632..32894b9 100644 --- a/src/lib/server/cookie.ts +++ b/src/lib/server/cookie.ts @@ -1,41 +1,36 @@ -import { getHonoContext } from "waku/unstable_hono"; import cookie from "cookie"; import db from "../db"; - import type { Middleware } from "waku/config"; const cookieMiddleware: Middleware = () => { - console.log("cookieMiddleware executed"); return async (ctx, next) => { + // Parse incoming cookies const cookies = cookie.parse(ctx.req.headers.cookie || ""); 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 cookie exists, fetch user data and set in context if (coki) { const userRow = db.fetchCookie(coki); - // console.log({ userRow }); - 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", - // }; - // } + 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(); - const hctx: any = getHonoContext(); - // console.log("hono", hctx.lol); - // console.log("ctx coki", ctx.data.cookie); - ctx.res.headers ||= {}; - if (ctx.data.cookie) - ctx.res.headers["set-cookie"] = ctx.data.cookie as string; - ctx.res.headers["set-lmao"] = "wtf man"; + + // Cookie setting is now handled by setCookieMiddleware }; }; -- cgit v1.2.3