Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Add flag to avoid weird errors when browser window has been already c… #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function (config, windowParams) {

return new Promise(function (resolve, reject) {
const authWindow = new BrowserWindow(windowParams || {'use-content-size': true});
let done = false;

authWindow.loadURL(url);
authWindow.show();
Expand All @@ -42,24 +43,31 @@ module.exports = function (config, windowParams) {
reject(new Error('window was closed by user'));
});

function close() {
authWindow.removeAllListeners('closed');

setImmediate(function() {
authWindow.close();
});
}

function onCallback(url) {
if (done) {
return;
}
done = true;

var url_parts = nodeUrl.parse(url, true);
var query = url_parts.query;
var code = query.code;
var error = query.error;

if (error !== undefined) {
reject(error);
authWindow.removeAllListeners('closed');
setImmediate(function () {
authWindow.close();
});
close();
} else if (code) {
resolve(code);
authWindow.removeAllListeners('closed');
setImmediate(function () {
authWindow.close();
});
close();
}
}

Expand Down