Skip to content

Commit

Permalink
feat: Allow web-login donecheck to cancel opener promise (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
jumoel committed Jun 8, 2022
1 parent 6bdd233 commit aa82de0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/index.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit aa82de0

Please sign in to comment.