1 var npm = require("./npm.js")
3 module.exports = whoami
5 whoami.usage = "npm whoami\n(just prints username according to given registry)"
7 function whoami (args, silent, cb) {
8 // FIXME: need tighter checking on this, but is a breaking change
9 if (typeof cb !== "function") {
14 var registry = npm.config.get("registry")
15 if (!registry) return cb(new Error("no default registry set"))
17 var auth = npm.config.getCredentialsByURI(registry)
20 if (!silent) console.log(auth.username)
21 return process.nextTick(cb.bind(this, null, auth.username))
23 else if (auth.token) {
24 return npm.registry.whoami(registry, { auth : auth }, function (er, username) {
27 var needNewSession = new Error(
28 "Your auth token is no longer valid. Please log in again."
30 needNewSession.code = 'ENEEDAUTH'
31 return cb(needNewSession)
34 if (!silent) console.log(username)
40 // At this point, if they have a credentials object, it doesn't have a token
41 // or auth in it. Probably just the default registry.
42 var needAuth = new Error(
43 "this command requires you to be logged in."
45 needAuth.code = 'ENEEDAUTH'
46 process.nextTick(cb.bind(this, needAuth))