22
33const fetch = require ( 'npm-registry-fetch' )
44const { HttpErrorBase } = require ( 'npm-registry-fetch/lib/errors' )
5+ const EventEmitter = require ( 'events' )
56const os = require ( 'os' )
67const { URL } = require ( 'url' )
78const log = require ( 'proc-log' )
@@ -59,6 +60,9 @@ const webAuth = (opener, opts, body) => {
5960 ...opts ,
6061 method : 'POST' ,
6162 body,
63+ headers : {
64+ 'npm-use-webauthn' : opts . authType === 'webauthn' ,
65+ } ,
6266 } ) . then ( res => {
6367 return Promise . all ( [ res , res . json ( ) ] )
6468 } ) . then ( ( [ res , content ] ) => {
@@ -70,8 +74,23 @@ const webAuth = (opener, opts, body) => {
7074 return content
7175 } ) . then ( ( { doneUrl, loginUrl } ) => {
7276 log . verbose ( 'web auth' , 'opening url pair' )
73- return opener ( loginUrl ) . then (
74- ( ) => webAuthCheckLogin ( doneUrl , { ...opts , cache : false } )
77+
78+ const doneEmitter = new EventEmitter ( )
79+
80+ const openPromise = opener ( loginUrl , doneEmitter )
81+ const webAuthCheckPromise = webAuthCheckLogin ( doneUrl , { ...opts , cache : false } )
82+ . then ( authResult => {
83+ log . verbose ( 'web auth' , 'done-check finished' )
84+
85+ // cancel open prompt if it's present
86+ doneEmitter . emit ( 'abort' )
87+
88+ return authResult
89+ } )
90+
91+ return Promise . all ( [ openPromise , webAuthCheckPromise ] ) . then (
92+ // pick the auth result and pass it along
93+ ( [ , authResult ] ) => authResult
7594 )
7695 } ) . catch ( er => {
7796 if ( ( er . statusCode >= 400 && er . statusCode <= 499 ) || er . statusCode === 500 ) {
0 commit comments