UNPKG

1.12 kBJavaScriptView Raw
1module.exports = logout
2
3var dezalgo = require('dezalgo')
4var log = require('npmlog')
5
6var npm = require('./npm.js')
7var mapToRegistry = require('./utils/map-to-registry.js')
8
9logout.usage = 'npm logout [--registry=<url>] [--scope=<@scope>]'
10
11function 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
20function logout (args, cb) {
21 cb = dezalgo(cb)
22
23 mapToRegistry('/', npm.config, function (err, uri, auth, normalized) {
24 if (err) return cb(err)
25
26 if (auth.token) {
27 log.verbose('logout', 'clearing session token for', normalized)
28 npm.registry.logout(normalized, { auth: auth }, function (err) {
29 if (err) return cb(err)
30
31 afterLogout(normalized, cb)
32 })
33 } else if (auth.username || auth.password) {
34 log.verbose('logout', 'clearing user credentials for', normalized)
35
36 afterLogout(normalized, cb)
37 } else {
38 cb(new Error(
39 'Not logged in to', normalized + ',', "so can't log out."
40 ))
41 }
42 })
43}