summaryrefslogtreecommitdiff
path: root/src/lib/server/cookiebridge.ts
diff options
context:
space:
mode:
authorpolwex <polwex@sortug.com>2025-05-29 14:52:38 +0700
committerpolwex <polwex@sortug.com>2025-05-29 14:52:38 +0700
commit490388360a0852bcf8ee054e96fa90e166df5792 (patch)
tree3940097c5505ff1bb09875dddb7ee0e4881beb77 /src/lib/server/cookiebridge.ts
parentf243847216279cbd43879de8b5ef6dcceb3a2f1d (diff)
fucker actually solved the cookies, love ya man
Diffstat (limited to 'src/lib/server/cookiebridge.ts')
-rw-r--r--src/lib/server/cookiebridge.ts56
1 files changed, 18 insertions, 38 deletions
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};`;
- }, "");
-}