Skip to content

Commit

Permalink
feat: intercepting cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
zamitto committed Dec 9, 2024
1 parent 0157d54 commit e978d84
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/renderer/src/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export function addCookieInterceptor() {
Object.defineProperty(document, "cookie", {
enumerable: true,
configurable: true,
get() {
const cookies = localStorage.getItem("cookies") || "";
console.log("get cookie", cookies);
return cookies;
},
set(cookieString) {
try {
console.log("setting cookie", cookieString);
const [cookieName, cookieValue] = cookieString.split(";")[0].split("=");

const currentCookies = localStorage.getItem("cookies") || "";

console.log("pre cookies obj", currentCookies);
const cookiesObject = parseCookieStringsToObjects(currentCookies);
cookiesObject[cookieName] = cookieValue;
console.log("cookiesObject", cookiesObject);

const newString = Object.entries(cookiesObject)
.map(([key, value]) => {
return key + "=" + value;
})
.join("; ");

console.log("set cookie", newString);
localStorage.setItem("cookies", newString);
} catch (err) {
console.error(err);
}
},
});
}

const parseCookieStringsToObjects = (
cookieStrings: string
): { [key: string]: string } => {
const result = {};

if (cookieStrings === "") return result;

console.log(cookieStrings);
cookieStrings.split(";").forEach((cookieString) => {
console.log("forEach", cookieString);
const [name, value] = cookieString.split("=");
result[name.trim()] = value.trim();
});

return result;
};
3 changes: 3 additions & 0 deletions src/renderer/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import resources from "@locales";
import { RepacksContextProvider } from "./context";
import { SuspenseWrapper } from "./components";
import { logger } from "./logger";
import { addCookieInterceptor } from "./cookies";

const Home = React.lazy(() => import("./pages/home/home"));
const GameDetails = React.lazy(
Expand All @@ -37,6 +38,8 @@ const Achievements = React.lazy(

console.log = logger.log;

addCookieInterceptor();

i18n
.use(LanguageDetector)
.use(initReactI18next)
Expand Down

0 comments on commit e978d84

Please sign in to comment.