11'use strict'
2+
23const read = require ( '../utils/read-user-info.js' )
34const profile = require ( 'npm-profile' )
45const log = require ( 'npmlog' )
5- const npm = require ( '../npm.js' )
6+ const figgyPudding = require ( 'figgy-pudding' )
7+ const npmConfig = require ( '../config/figgy-config.js' )
68const output = require ( '../utils/output.js' )
7- const pacoteOpts = require ( '../config/pacote' )
8- const fetchOpts = require ( '../config/fetch-opts' )
99const openUrl = require ( '../utils/open-url' )
1010
1111const openerPromise = ( url ) => new Promise ( ( resolve , reject ) => {
@@ -26,54 +26,54 @@ const loginPrompter = (creds) => {
2626 } )
2727}
2828
29- module . exports . login = ( creds , registry , scope , cb ) => {
30- const conf = {
31- log : log ,
32- creds : creds ,
33- registry : registry ,
34- auth : {
35- otp : npm . config . get ( 'otp' )
36- } ,
37- scope : scope ,
38- opts : fetchOpts . fromPacote ( pacoteOpts ( ) )
39- }
40- login ( conf ) . then ( ( newCreds ) => cb ( null , newCreds ) ) . catch ( cb )
29+ const LoginOpts = figgyPudding ( {
30+ 'always-auth' : { } ,
31+ creds : { } ,
32+ log : { default : ( ) => log } ,
33+ registry : { } ,
34+ scope : { }
35+ } )
36+
37+ module . exports . login = ( creds = { } , registry , scope , cb ) => {
38+ const opts = LoginOpts ( npmConfig ( ) ) . concat ( { scope, registry, creds} )
39+ login ( opts ) . then ( ( newCreds ) => cb ( null , newCreds ) ) . catch ( cb )
4140}
4241
43- function login ( conf ) {
44- return profile . login ( openerPromise , loginPrompter , conf )
42+ function login ( opts ) {
43+ return profile . login ( openerPromise , loginPrompter , opts )
4544 . catch ( ( err ) => {
4645 if ( err . code === 'EOTP' ) throw err
47- const u = conf . creds . username
48- const p = conf . creds . password
49- const e = conf . creds . email
46+ const u = opts . creds . username
47+ const p = opts . creds . password
48+ const e = opts . creds . email
5049 if ( ! ( u && p && e ) ) throw err
51- return profile . adduserCouch ( u , e , p , conf )
50+ return profile . adduserCouch ( u , e , p , opts )
5251 } )
5352 . catch ( ( err ) => {
5453 if ( err . code !== 'EOTP' ) throw err
55- return read . otp ( 'Enter one-time password from your authenticator app: ' ) . then ( ( otp ) => {
56- conf . auth . otp = otp
57- const u = conf . creds . username
58- const p = conf . creds . password
59- return profile . loginCouch ( u , p , conf )
54+ return read . otp (
55+ 'Enter one-time password from your authenticator app: '
56+ ) . then ( otp => {
57+ const u = opts . creds . username
58+ const p = opts . creds . password
59+ return profile . loginCouch ( u , p , opts . concat ( { otp} ) )
6060 } )
6161 } ) . then ( ( result ) => {
6262 const newCreds = { }
6363 if ( result && result . token ) {
6464 newCreds . token = result . token
6565 } else {
66- newCreds . username = conf . creds . username
67- newCreds . password = conf . creds . password
68- newCreds . email = conf . creds . email
69- newCreds . alwaysAuth = npm . config . get ( 'always-auth' )
66+ newCreds . username = opts . creds . username
67+ newCreds . password = opts . creds . password
68+ newCreds . email = opts . creds . email
69+ newCreds . alwaysAuth = opts [ 'always-auth' ]
7070 }
7171
72- const usermsg = conf . creds . username ? ' user ' + conf . creds . username : ''
73- conf . log . info ( 'login' , 'Authorized' + usermsg )
74- const scopeMessage = conf . scope ? ' to scope ' + conf . scope : ''
75- const userout = conf . creds . username ? ' as ' + conf . creds . username : ''
76- output ( 'Logged in%s%s on %s.' , userout , scopeMessage , conf . registry )
72+ const usermsg = opts . creds . username ? ' user ' + opts . creds . username : ''
73+ opts . log . info ( 'login' , 'Authorized' + usermsg )
74+ const scopeMessage = opts . scope ? ' to scope ' + opts . scope : ''
75+ const userout = opts . creds . username ? ' as ' + opts . creds . username : ''
76+ output ( 'Logged in%s%s on %s.' , userout , scopeMessage , opts . registry )
7777 return newCreds
7878 } )
7979}
0 commit comments