Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 460ed21

Browse files
wyzeiarna
authored andcommitted
logout: Fix npm-logout to remove scope config if specified
Fixes: #10529 Credit: @wyze Reviewed-By: @iarna PR-URL: #11417
1 parent d1d0233 commit 460ed21

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

lib/logout.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ var mapToRegistry = require('./utils/map-to-registry.js')
88

99
logout.usage = 'npm logout [--registry=<url>] [--scope=<@scope>]'
1010

11+
function afterLogout (normalized, cb) {
12+
var scope = npm.config.get('scope')
13+
14+
if (scope) npm.config.del(scope + ':registry')
15+
16+
npm.config.clearCredentialsByURI(normalized)
17+
npm.config.save('user', cb)
18+
}
19+
1120
function logout (args, cb) {
1221
cb = dezalgo(cb)
1322

@@ -19,13 +28,12 @@ function logout (args, cb) {
1928
npm.registry.logout(normalized, { auth: auth }, function (err) {
2029
if (err) return cb(err)
2130

22-
npm.config.clearCredentialsByURI(normalized)
23-
npm.config.save('user', cb)
31+
afterLogout(normalized, cb)
2432
})
2533
} else if (auth.username || auth.password) {
2634
log.verbose('logout', 'clearing user credentials for', normalized)
27-
npm.config.clearCredentialsByURI(normalized)
28-
npm.config.save('user', cb)
35+
36+
afterLogout(normalized, cb)
2937
} else {
3038
cb(new Error(
3139
'Not logged in to', normalized + ',', "so can't log out."

test/tap/logout-scoped.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
4+
var mkdirp = require('mkdirp')
5+
var mr = require('npm-registry-mock')
6+
var rimraf = require('rimraf')
7+
var test = require('tap').test
8+
9+
var common = require('../common-tap.js')
10+
11+
var pkg = path.resolve(__dirname, 'logout')
12+
var outfile = path.join(pkg, '_npmrc')
13+
var opts = { cwd: pkg }
14+
15+
var contents = function () {/*
16+
foo=boo
17+
@bar:registry=http://localhost:1337
18+
//localhost:1337/:_authToken=glarb
19+
*/}.toString().split('\n').slice(1, -1).join('\n')
20+
21+
function mocks (server) {
22+
server.delete('/-/user/token/glarb')
23+
.reply(200, {})
24+
}
25+
26+
test('setup', function (t) {
27+
cleanup()
28+
setup()
29+
t.end()
30+
})
31+
32+
test('npm logout', function (t) {
33+
mr({ port: common.port, plugin: mocks }, function (err, s) {
34+
if (err) throw err
35+
36+
common.npm(
37+
[
38+
'logout',
39+
'--registry', common.registry,
40+
'--scope', '@bar',
41+
'--loglevel', 'silent',
42+
'--userconfig', outfile
43+
],
44+
opts,
45+
function (err, code) {
46+
t.ifError(err, 'no error output')
47+
t.notOk(code, 'exited OK')
48+
49+
var config = fs.readFileSync(outfile, 'utf8')
50+
t.equal(config, 'foo=boo\n', 'creds gone')
51+
s.close()
52+
t.end()
53+
}
54+
)
55+
})
56+
})
57+
58+
test('cleanup', function (t) {
59+
cleanup()
60+
t.end()
61+
})
62+
63+
function setup () {
64+
mkdirp.sync(pkg)
65+
fs.writeFileSync(outfile, contents)
66+
}
67+
68+
function cleanup () {
69+
rimraf.sync(pkg)
70+
}

0 commit comments

Comments
 (0)