From 586df616e8d4a19148b74f71f673c2baaae1d2bb Mon Sep 17 00:00:00 2001 From: Chubby Granny Chaser Date: Sat, 28 Sep 2024 00:59:17 +0100 Subject: [PATCH] feat: removing session interception from auth --- src/main/index.ts | 42 +------------------------- src/main/services/window-manager.ts | 47 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index c9e36b2c..9127b881 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, net, protocol, session } from "electron"; +import { app, BrowserWindow, net, protocol } from "electron"; import { init } from "@sentry/electron/main"; import updater from "electron-updater"; import i18n from "i18next"; @@ -103,46 +103,6 @@ app.whenReady().then(async () => { WindowManager.createMainWindow(); WindowManager.createSystemTray(userPreferences?.language || "en"); - - session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => { - callback({ - requestHeaders: { - ...details.requestHeaders, - "user-agent": - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", - }, - }); - }); - - session.defaultSession.webRequest.onHeadersReceived((details, callback) => { - const headers = { - "access-control-allow-origin": ["*"], - "access-control-allow-methods": ["GET, POST, PUT, DELETE, OPTIONS"], - "access-control-expose-headers": ["ETag"], - "access-control-allow-headers": [ - "Content-Type, Authorization, X-Requested-With, If-None-Match", - ], - "access-control-allow-credentials": ["true"], - }; - - if (details.method === "OPTIONS") { - callback({ - cancel: false, - responseHeaders: { - ...details.responseHeaders, - ...headers, - }, - statusLine: "HTTP/1.1 200 OK", - }); - } else { - callback({ - responseHeaders: { - ...details.responseHeaders, - ...headers, - }, - }); - } - }); }); app.on("browser-window-created", (_, window) => { diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index cf904fb4..4af15c82 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -61,6 +61,53 @@ export class WindowManager { show: false, }); + this.mainWindow.webContents.session.webRequest.onBeforeSendHeaders( + (details, callback) => { + callback({ + requestHeaders: { + ...details.requestHeaders, + "user-agent": + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", + }, + }); + } + ); + + this.mainWindow.webContents.session.webRequest.onHeadersReceived( + (details, callback) => { + if (details.webContentsId !== this.mainWindow?.webContents.id) { + return callback(details); + } + + const headers = { + "access-control-allow-origin": ["*"], + "access-control-allow-methods": ["GET, POST, PUT, DELETE, OPTIONS"], + "access-control-expose-headers": ["ETag"], + "access-control-allow-headers": [ + "Content-Type, Authorization, X-Requested-With, If-None-Match", + ], + }; + + if (details.method === "OPTIONS") { + return callback({ + cancel: false, + responseHeaders: { + ...details.responseHeaders, + ...headers, + }, + statusLine: "HTTP/1.1 200 OK", + }); + } + + return callback({ + responseHeaders: { + ...details.responseHeaders, + ...headers, + }, + }); + } + ); + this.loadURL(); this.mainWindow.removeMenu();