Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 763f525

Browse files
committedFeb 10, 2023
auth-type: gracefully fallback from web
Originally `auth-type=web` would gracefully fallback to `auth-type=legacy` if a registry had not yet implemented web. This brings back that behavior.
1 parent c606653 commit 763f525

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed
 

‎lib/utils/auth.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ const openUrlPrompt = require('../utils/open-url-prompt.js')
44
const read = require('../utils/read-user-info.js')
55
const otplease = require('../utils/otplease.js')
66

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+
727
const adduser = async (npm, { creds, ...opts }) => {
828
const authType = npm.config.get('auth-type')
929
let res
@@ -16,17 +36,16 @@ const adduser = async (npm, { creds, ...opts }) => {
1636
'Press ENTER to open in the browser...',
1737
emitter
1838
)
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+
})
2047
} 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)
3049
}
3150

3251
// 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 }) => {
5271
'Press ENTER to open in the browser...',
5372
emitter
5473
)
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+
})
5682
} 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)
6284
}
6385

6486
// We don't know the username if it was a web login, all we can reliably log is scope and registry

0 commit comments

Comments
 (0)
Please sign in to comment.