const BB = require('bluebird')
const npmConfig = require('./config/figgy-config.js')
const fetch = require('libnpm/fetch')
const figgyPudding = require('figgy-pudding')
const npm = require('./npm.js')
const output = require('./utils/output.js')
const WhoamiConfig = figgyPudding({
whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'
function whoami ([spec], silent, cb) {
// FIXME: need tighter checking on this, but is a breaking change
if (typeof cb !== 'function') {
const opts = WhoamiConfig(npmConfig())
// First, check if we have a user/pass-based auth
const registry = opts.registry
if (!registry) throw new Error('no default registry set')
return npm.config.getCredentialsByURI(registry)
}).then(({username, token}) => {
return fetch.json('/-/whoami', opts.concat({
})).then(({username}) => {
throw Object.assign(new Error(
'Your auth token is no longer valid. Please log in again.'
// At this point, if they have a credentials object, it doesn't have a
// token or auth in it. Probably just the default registry.
throw Object.assign(new Error(
'This command requires you to be logged in.'
output(JSON.stringify(username))