logout.js 1.3 KB

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