summaryrefslogtreecommitdiff
path: root/deps/npm/lib/logout.js
blob: 411f547210b8f1954c19bf1604e166224b9e2bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict'

const BB = require('bluebird')

const eu = encodeURIComponent
const getAuth = require('npm-registry-fetch/auth.js')
const log = require('npmlog')
const npm = require('./npm.js')
const npmConfig = require('./config/figgy-config.js')
const npmFetch = require('libnpm/fetch')

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

function afterLogout (normalized) {
  var scope = npm.config.get('scope')

  if (scope) npm.config.del(scope + ':registry')

  npm.config.clearCredentialsByURI(normalized)
  return BB.fromNode(cb => npm.config.save('user', cb))
}

module.exports = logout
function logout (args, cb) {
  const opts = npmConfig()
  BB.try(() => {
    const reg = npmFetch.pickRegistry('foo', opts)
    const auth = getAuth(reg, opts)
    if (auth.token) {
      log.verbose('logout', 'clearing session token for', reg)
      return npmFetch(`/-/user/token/${eu(auth.token)}`, opts.concat({
        method: 'DELETE',
        ignoreBody: true
      })).then(() => afterLogout(reg))
    } else if (auth.username || auth.password) {
      log.verbose('logout', 'clearing user credentials for', reg)
      return afterLogout(reg)
    } else {
      throw new Error(
        'Not logged in to', reg + ',', "so can't log out."
      )
    }
  }).nodeify(cb)
}