Skip to content

Instantly share code, notes, and snippets.

@paxti
Created February 4, 2018 04:14
Show Gist options
  • Save paxti/1f577c2eb11ca7b27755867cc1a50d68 to your computer and use it in GitHub Desktop.
Save paxti/1f577c2eb11ca7b27755867cc1a50d68 to your computer and use it in GitHub Desktop.
Modified to work in Windows env index.js for Electron in https://github.com/darkyen/pkce-demos
const {BrowserWindow, app, protocol, ipcMain} = require('electron');
const env = require('../env');
const path = require('path')
var mainWindow = null;
function _generateResponse () {
return `
<html>
<head></head>
<body>
<h1>Authentication Complete</h1>
<p>You can close this page now</p>
</body>
</html>
`;
}
const isSecondInstance = app.makeSingleInstance((args, _) => {
mainWindow.webContents.executeJavaScript(`
window.handleOpenURL('${args[2]}');
`);
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
}
)
if (isSecondInstance) {
app.quit()
}
app.on('ready', function() {
mainWindow = new BrowserWindow({
height: 600,
width: 800
});
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
mainWindow.webContents.openDevTools();
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(env.packageIdentifier, process.execPath, [path.resolve(process.argv[1])])
}
} else {
app.setAsDefaultProtocolClient(env.packageIdentifier)
}
app.on('open-url', function(event, url){
mainWindow.webContents.executeJavaScript(`
window.handleOpenURL('${url}');
`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment