2015-10-10 08:13:57 +02:00
|
|
|
var npm = require('./npm.js')
|
2016-06-24 22:43:51 +02:00
|
|
|
var output = require('./utils/output.js')
|
2011-11-21 18:48:45 +01:00
|
|
|
|
2014-09-24 23:41:07 +02:00
|
|
|
module.exports = whoami
|
|
|
|
|
2015-10-10 08:13:57 +02:00
|
|
|
whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'
|
2011-11-21 18:48:45 +01:00
|
|
|
|
2012-07-17 20:37:39 +02:00
|
|
|
function whoami (args, silent, cb) {
|
2014-09-24 23:41:07 +02:00
|
|
|
// FIXME: need tighter checking on this, but is a breaking change
|
2015-10-10 08:13:57 +02:00
|
|
|
if (typeof cb !== 'function') {
|
2014-09-24 23:41:07 +02:00
|
|
|
cb = silent
|
|
|
|
silent = false
|
|
|
|
}
|
|
|
|
|
2015-10-10 08:13:57 +02:00
|
|
|
var registry = npm.config.get('registry')
|
|
|
|
if (!registry) return cb(new Error('no default registry set'))
|
2014-09-24 23:41:07 +02:00
|
|
|
|
2015-01-08 23:37:26 +01:00
|
|
|
var auth = npm.config.getCredentialsByURI(registry)
|
|
|
|
if (auth) {
|
|
|
|
if (auth.username) {
|
2016-06-24 22:43:51 +02:00
|
|
|
if (!silent) output(auth.username)
|
2015-01-08 23:37:26 +01:00
|
|
|
return process.nextTick(cb.bind(this, null, auth.username))
|
2015-10-10 08:13:57 +02:00
|
|
|
} else if (auth.token) {
|
|
|
|
return npm.registry.whoami(registry, { auth: auth }, function (er, username) {
|
2014-09-24 23:41:07 +02:00
|
|
|
if (er) return cb(er)
|
2015-03-13 10:07:27 +01:00
|
|
|
if (!username) {
|
|
|
|
var needNewSession = new Error(
|
2015-10-10 08:13:57 +02:00
|
|
|
'Your auth token is no longer valid. Please log in again.'
|
2015-03-13 10:07:27 +01:00
|
|
|
)
|
|
|
|
needNewSession.code = 'ENEEDAUTH'
|
|
|
|
return cb(needNewSession)
|
|
|
|
}
|
2014-09-24 23:41:07 +02:00
|
|
|
|
2016-06-24 22:43:51 +02:00
|
|
|
if (!silent) output(username)
|
2014-09-24 23:41:07 +02:00
|
|
|
cb(null, username)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 10:07:27 +01:00
|
|
|
// At this point, if they have a credentials object, it doesn't have a token
|
|
|
|
// or auth in it. Probably just the default registry.
|
|
|
|
var needAuth = new Error(
|
2015-10-10 08:13:57 +02:00
|
|
|
'this command requires you to be logged in.'
|
2015-03-13 10:07:27 +01:00
|
|
|
)
|
|
|
|
needAuth.code = 'ENEEDAUTH'
|
|
|
|
process.nextTick(cb.bind(this, needAuth))
|
2011-11-21 18:48:45 +01:00
|
|
|
}
|