Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#194: Frontend onBeforeSendHeaders / include credentials #195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions Electron/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,6 @@ const createWindow = async () => {
if (!mainWindow) {
throw new Error('"mainWindow" is not defined');
}
const ses = mainWindow.webContents.session;

// Interceptamos las solicitudes web
ses.webRequest.onBeforeSendHeaders(async (details, callback) => {
// Add credentials to all requests
try {
const cookies = await ses.cookies.get({});

const filteredJwtTokens = cookies.filter((cookie) => {
return cookie.name === 'jwt';
});

if (filteredJwtTokens && filteredJwtTokens[0]) {
details.requestHeaders.Authorization =
filteredJwtTokens[filteredJwtTokens.length - 1].value;
}

callback({ cancel: false, requestHeaders: details.requestHeaders });
} catch (error) {
console.error('Error getting cookies:', error);
callback({ cancel: false, requestHeaders: details.requestHeaders });
}
});

if (process.env.START_MINIMIZED) {
mainWindow.minimize();
Expand Down
29 changes: 29 additions & 0 deletions Electron/src/main/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint import/prefer-default-export: off */
import { URL } from 'url';
import path from 'path';
import { BrowserWindow } from 'electron';

export function resolveHtmlPath(htmlFileName: string) {
if (process.env.NODE_ENV === 'development') {
Expand All @@ -11,3 +12,31 @@ export function resolveHtmlPath(htmlFileName: string) {
}
return `file://${path.resolve(__dirname, '../renderer/', htmlFileName)}`;
}

/* Intercept request headers and insert cookies. It has to be called after `ready-to-show` event */
export const interceptRequestHeaders = (mainWindow: BrowserWindow) => {
if (!mainWindow) return;
const ses = mainWindow.webContents.session;

// Interceptamos las solicitudes web
ses.webRequest.onBeforeSendHeaders(async (details, callback) => {
// Add credentials to all requests
try {
const cookies = await ses.cookies.get({});

const filteredJwtTokens = cookies.filter((cookie) => {
return cookie.name === 'jwt';
});

if (filteredJwtTokens && filteredJwtTokens[0]) {
details.requestHeaders.Authorization =
filteredJwtTokens[filteredJwtTokens.length - 1].value;
}

callback({ cancel: false, requestHeaders: details.requestHeaders });
} catch (error) {
console.error('Error getting cookies:', error);
callback({ cancel: false, requestHeaders: details.requestHeaders });
}
});
};