• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5const eu = encodeURIComponent
6const getAuth = require('npm-registry-fetch/auth.js')
7const log = require('npmlog')
8const npm = require('./npm.js')
9const npmConfig = require('./config/figgy-config.js')
10const npmFetch = require('libnpm/fetch')
11
12logout.usage = 'npm logout [--registry=<url>] [--scope=<@scope>]'
13
14function afterLogout (normalized) {
15  var scope = npm.config.get('scope')
16
17  if (scope) npm.config.del(scope + ':registry')
18
19  npm.config.clearCredentialsByURI(normalized)
20  return BB.fromNode(cb => npm.config.save('user', cb))
21}
22
23module.exports = logout
24function logout (args, cb) {
25  const opts = npmConfig()
26  BB.try(() => {
27    const reg = npmFetch.pickRegistry('foo', opts)
28    const auth = getAuth(reg, opts)
29    if (auth.token) {
30      log.verbose('logout', 'clearing session token for', reg)
31      return npmFetch(`/-/user/token/${eu(auth.token)}`, opts.concat({
32        method: 'DELETE',
33        ignoreBody: true
34      })).then(() => afterLogout(reg))
35    } else if (auth.username || auth.password) {
36      log.verbose('logout', 'clearing user credentials for', reg)
37      return afterLogout(reg)
38    } else {
39      throw new Error(
40        'Not logged in to', reg + ',', "so can't log out."
41      )
42    }
43  }).nodeify(cb)
44}
45