Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow the done-check for web login to cancel opener promise #50

Merged
merged 1 commit into from Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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