summaryrefslogtreecommitdiff
path: root/src/lib/server/cookie.ts
blob: bbabd639434d8b6d23df596ba10dee128b0223c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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) => {
    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 (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",
      //     };
      //   }
    }
    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";
  };
};

export default cookieMiddleware;