@@ -4,6 +4,26 @@ const openUrlPrompt = require('../utils/open-url-prompt.js')
4
4
const read = require ( '../utils/read-user-info.js' )
5
5
const otplease = require ( '../utils/otplease.js' )
6
6
7
+ async function loginCouch ( npm , creds , opts ) {
8
+ const username = await read . username ( 'Username:' , creds . username )
9
+ const password = await read . password ( 'Password:' , creds . password )
10
+ return await otplease ( npm , opts , ( reqOpts ) =>
11
+ profile . loginCouch ( username , password , reqOpts )
12
+ )
13
+ }
14
+
15
+ async function addUserCouch ( npm , creds , opts ) {
16
+ const username = await read . username ( 'Username:' , creds . username )
17
+ const password = await read . password ( 'Password:' , creds . password )
18
+ const email = await read . email ( 'Email: (this IS public) ' , creds . email )
19
+ // npm registry quirk: If you "add" an existing user with their current
20
+ // password, it's effectively a login, and if that account has otp you'll
21
+ // be prompted for it.
22
+ return await otplease ( npm , opts , ( reqOpts ) =>
23
+ profile . adduserCouch ( username , email , password , opts )
24
+ )
25
+ }
26
+
7
27
const adduser = async ( npm , { creds, ...opts } ) => {
8
28
const authType = npm . config . get ( 'auth-type' )
9
29
let res
@@ -16,17 +36,16 @@ const adduser = async (npm, { creds, ...opts }) => {
16
36
'Press ENTER to open in the browser...' ,
17
37
emitter
18
38
)
19
- } , opts )
39
+ } , opts ) . catch ( er => {
40
+ if ( er . message === 'Web login not supported' && er . code === 'ENYI' ) {
41
+ log . verbose ( 'web add user not supported, trying couch' )
42
+ return addUserCouch ( npm , creds , opts )
43
+ } else {
44
+ throw er
45
+ }
46
+ } )
20
47
} else {
21
- const username = await read . username ( 'Username:' , creds . username )
22
- const password = await read . password ( 'Password:' , creds . password )
23
- const email = await read . email ( 'Email: (this IS public) ' , creds . email )
24
- // npm registry quirk: If you "add" an existing user with their current
25
- // password, it's effectively a login, and if that account has otp you'll
26
- // be prompted for it.
27
- res = await otplease ( npm , opts , ( reqOpts ) =>
28
- profile . adduserCouch ( username , email , password , opts )
29
- )
48
+ res = addUserCouch ( npm , creds , opts )
30
49
}
31
50
32
51
// We don't know the username if it was a web login, all we can reliably log is scope and registry
@@ -52,13 +71,16 @@ const login = async (npm, { creds, ...opts }) => {
52
71
'Press ENTER to open in the browser...' ,
53
72
emitter
54
73
)
55
- } , opts )
74
+ } , opts ) . catch ( er => {
75
+ if ( er . message === 'Web login not supported' && er . code === 'ENYI' ) {
76
+ log . verbose ( 'web login not supported, trying couch' )
77
+ return loginCouch ( npm , creds , opts )
78
+ } else {
79
+ throw er
80
+ }
81
+ } )
56
82
} else {
57
- const username = await read . username ( 'Username:' , creds . username )
58
- const password = await read . password ( 'Password:' , creds . password )
59
- res = await otplease ( npm , opts , ( reqOpts ) =>
60
- profile . loginCouch ( username , password , reqOpts )
61
- )
83
+ res = loginCouch ( npm , creds , opts )
62
84
}
63
85
64
86
// We don't know the username if it was a web login, all we can reliably log is scope and registry
0 commit comments