diff --git a/lib/index.js b/lib/index.js index 8de1722..eb97c62 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ const fetch = require('npm-registry-fetch') const { HttpErrorBase } = require('npm-registry-fetch/lib/errors') +const EventEmitter = require('events') const os = require('os') const { URL } = require('url') const log = require('proc-log') @@ -73,8 +74,23 @@ const webAuth = (opener, opts, body) => { return content }).then(({ doneUrl, loginUrl }) => { log.verbose('web auth', 'opening url pair') - return opener(loginUrl).then( - () => webAuthCheckLogin(doneUrl, { ...opts, cache: false }) + + const doneEmitter = new EventEmitter() + + const openPromise = opener(loginUrl, doneEmitter) + const webAuthCheckPromise = webAuthCheckLogin(doneUrl, { ...opts, cache: false }) + .then(authResult => { + log.verbose('web auth', 'done-check finished') + + // cancel open prompt if it's present + doneEmitter.emit('abort') + + return authResult + }) + + return Promise.all([openPromise, webAuthCheckPromise]).then( + // pick the auth result and pass it along + ([, authResult]) => authResult ) }).catch(er => { if ((er.statusCode >= 400 && er.statusCode <= 499) || er.statusCode === 500) {