adduser.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module.exports = adduser
  2. const log = require('npmlog')
  3. const npm = require('./npm.js')
  4. const usage = require('./utils/usage')
  5. let crypto
  6. try {
  7. crypto = require('crypto')
  8. } catch (ex) {}
  9. adduser.usage = usage(
  10. 'adduser',
  11. 'npm adduser [--registry=url] [--scope=@orgname] [--auth-type=legacy] [--always-auth]'
  12. )
  13. function adduser (args, cb) {
  14. if (!crypto) {
  15. return cb(new Error(
  16. 'You must compile node with ssl support to use the adduser feature'
  17. ))
  18. }
  19. let registry = npm.config.get('registry')
  20. const scope = npm.config.get('scope')
  21. const creds = npm.config.getCredentialsByURI(npm.config.get('registry'))
  22. if (scope) {
  23. const scopedRegistry = npm.config.get(scope + ':registry')
  24. const cliRegistry = npm.config.get('registry', 'cli')
  25. if (scopedRegistry && !cliRegistry) registry = scopedRegistry
  26. }
  27. log.disableProgress()
  28. let auth
  29. try {
  30. auth = require('./auth/' + npm.config.get('auth-type'))
  31. } catch (e) {
  32. return cb(new Error('no such auth module'))
  33. }
  34. auth.login(creds, registry, scope, function (err, newCreds) {
  35. if (err) return cb(err)
  36. npm.config.del('_token', 'user') // prevent legacy pollution
  37. if (scope) npm.config.set(scope + ':registry', registry, 'user')
  38. npm.config.setCredentialsByURI(registry, newCreds)
  39. npm.config.save('user', cb)
  40. })
  41. }