summaryrefslogtreecommitdiff
path: root/src/lib/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/server')
-rw-r--r--src/lib/server/cookie.ts47
-rw-r--r--src/lib/server/cookiebridge.ts56
-rw-r--r--src/lib/server/setcookie.ts11
3 files changed, 49 insertions, 65 deletions
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
};
};
diff --git a/src/lib/server/cookiebridge.ts b/src/lib/server/cookiebridge.ts
index 778fc2c..613479b 100644
--- a/src/lib/server/cookiebridge.ts
+++ b/src/lib/server/cookiebridge.ts
@@ -1,52 +1,32 @@
import { getContext, getContextData } from "waku/middleware/context";
+import cookie from "cookie";
const useCookies = () => {
const ctx = getContext();
const headers = ctx.req.headers;
- console.log(headers.cookie);
-
- const getCookie = (s: string) => {
- const coki = headers.cookie;
- if (!coki) return {};
- const cokiMap = parseCoki(coki);
- return cokiMap;
+
+ const getCookie = (name: string) => {
+ const cookieHeader = headers.cookie;
+ if (!cookieHeader) return null;
+
+ const cookies = cookie.parse(cookieHeader);
+ return cookies[name];
};
- const setCookie = (s: string) => {
+
+ const setCookie = (value: string) => {
const ctxdata = getContextData();
- // (ctxdata.cokimap as Record<string, string>).sorlang = s;
- // ctxdata.cookie = `sorlang=${s}; Secure`
- ctxdata.cookie = `sorlang=${s};`;
+ // Set cookie with proper attributes for security and functionality
+ ctxdata.cookie = `sorlang=${value}; Path=/; HttpOnly; SameSite=Lax; Max-Age=2592000`;
+ console.log("Cookie value being set:", value);
};
- const delCookie = (s: string) => {
+
+ const delCookie = (name: string) => {
const ctxdata = getContextData();
- delete (ctxdata.cokimap as Record<string, string>).sorlang;
+ // Set an expired cookie to delete it
+ ctxdata.cookie = `${name}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT`;
};
+
return { getCookie, setCookie, delCookie };
};
export { useCookies };
-
-function parseCoki(s: string) {
- return s
- .split(";")
- .map((v) => v.split("="))
- .reduce((acc: Record<string, string>, v: any) => {
- acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
- return acc;
- }, {});
-}
-function parseSetCoki(s: string) {
- return s
- .split(";")
- .map((v) => v.split("="))
- .reduce((acc: Record<string, string>, v: any) => {
- acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
- return acc;
- }, {});
-}
-function cokiToString(m: Record<string, string>): string {
- return Object.entries(m).reduce((acc: string, item: [string, string]) => {
- const [key, val] = item;
- return `${acc} ${key}=${val};`;
- }, "");
-}
diff --git a/src/lib/server/setcookie.ts b/src/lib/server/setcookie.ts
index 61da128..10ca489 100644
--- a/src/lib/server/setcookie.ts
+++ b/src/lib/server/setcookie.ts
@@ -3,8 +3,17 @@ import type { Middleware } from "waku/config";
const setCookieMiddleware: Middleware = () => {
return async (ctx, next) => {
await next();
+
+ // Ensure headers object exists
ctx.res.headers ||= {};
- ctx.res.headers["set-cookie"] = ctx.data.cookie as string;
+
+ // Only set the cookie header if we have a cookie to set
+ if (ctx.data.cookie) {
+ ctx.res.headers["set-cookie"] = ctx.data.cookie as string;
+
+ // Debugging
+ console.log("Setting cookie header:", ctx.data.cookie);
+ }
};
};