UNPKG

1.46 kBJavaScriptView Raw
1var npm = require('./npm.js')
2var output = require('./utils/output.js')
3
4module.exports = whoami
5
6whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'
7
8function whoami (args, silent, cb) {
9 // FIXME: need tighter checking on this, but is a breaking change
10 if (typeof cb !== 'function') {
11 cb = silent
12 silent = false
13 }
14
15 var registry = npm.config.get('registry')
16 if (!registry) return cb(new Error('no default registry set'))
17
18 var auth = npm.config.getCredentialsByURI(registry)
19 if (auth) {
20 if (auth.username) {
21 if (!silent) output(auth.username)
22 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) {
25 if (er) return cb(er)
26 if (!username) {
27 var needNewSession = new Error(
28 'Your auth token is no longer valid. Please log in again.'
29 )
30 needNewSession.code = 'ENEEDAUTH'
31 return cb(needNewSession)
32 }
33
34 if (!silent) output(username)
35 cb(null, username)
36 })
37 }
38 }
39
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.'
44 )
45 needAuth.code = 'ENEEDAUTH'
46 process.nextTick(cb.bind(this, needAuth))
47}