Skip to content

Instantly share code, notes, and snippets.

@xprudhomme
Last active March 27, 2020 22:54
Show Gist options
  • Save xprudhomme/d737ddd6fe7bc0c24e0e12d415d263ac to your computer and use it in GitHub Desktop.
Save xprudhomme/d737ddd6fe7bc0c24e0e12d415d263ac to your computer and use it in GitHub Desktop.
auth-process.js
const {BrowserWindow, protocol} = require('electron');
const authService = require('../services/auth-service');
const createAppWindow = require('../main/app-process');
let win = null;
function createAuthWindow() {
destroyAuthWin();
// Create the browser window.
win = new BrowserWindow({
width: 1000,
height: 600,
});
win.loadURL(authService.getAuthenticationURL());
protocol.interceptFileProtocol("file", async ({url}) => {
if(!url.includes("file:///callback?code")){
return;
}
console.log("I'm in the interceptFileProtocol callback !! With:", url);
await authService.loadTokens(url);
protocol.uninterceptProtocol("file");
createAppWindow();
return destroyAuthWin();
});
win.on('authenticated', () => {
destroyAuthWin();
});
win.on('closed', () => {
win = null;
});
}
function destroyAuthWin() {
if (!win) return;
win.close();
win = null;
}
function createLogoutWindow() {
return new Promise(resolve => {
const logoutWindow = new BrowserWindow({
show: false,
});
logoutWindow.loadURL(authService.getLogOutUrl());
logoutWindow.on('ready-to-show', async () => {
logoutWindow.close();
await authService.logout();
resolve();
});
});
}
module.exports = {
createAuthWindow,
createLogoutWindow,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment