summaryrefslogtreecommitdiff
path: root/src/lib/server/cookiebridge.ts
blob: 613479bb296cf650a4c5b29b05caa885e6dde701 (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
import { getContext, getContextData } from "waku/middleware/context";
import cookie from "cookie";

const useCookies = () => {
  const ctx = getContext();
  const headers = ctx.req.headers;
  
  const getCookie = (name: string) => {
    const cookieHeader = headers.cookie;
    if (!cookieHeader) return null;
    
    const cookies = cookie.parse(cookieHeader);
    return cookies[name];
  };
  
  const setCookie = (value: string) => {
    const ctxdata = getContextData();
    // 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 = (name: string) => {
    const ctxdata = getContextData();
    // 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 };