1const getAuth = require('npm-registry-fetch/lib/auth.js') 2const npmFetch = require('npm-registry-fetch') 3const log = require('../utils/log-shim') 4const BaseCommand = require('../base-command.js') 5 6class Logout extends BaseCommand { 7 static description = 'Log out of the registry' 8 static name = 'logout' 9 static params = [ 10 'registry', 11 'scope', 12 ] 13 14 async exec (args) { 15 const registry = this.npm.config.get('registry') 16 const scope = this.npm.config.get('scope') 17 const regRef = scope ? `${scope}:registry` : 'registry' 18 const reg = this.npm.config.get(regRef) || registry 19 20 const auth = getAuth(reg, this.npm.flatOptions) 21 22 if (auth.token) { 23 log.verbose('logout', `clearing token for ${reg}`) 24 await npmFetch(`/-/user/token/${encodeURIComponent(auth.token)}`, { 25 ...this.npm.flatOptions, 26 method: 'DELETE', 27 ignoreBody: true, 28 }) 29 } else if (auth.isBasicAuth) { 30 log.verbose('logout', `clearing user credentials for ${reg}`) 31 } else { 32 const msg = `not logged in to ${reg}, so can't log out!` 33 throw Object.assign(new Error(msg), { code: 'ENEEDAUTH' }) 34 } 35 36 if (scope) { 37 this.npm.config.delete(regRef, 'user') 38 } 39 40 this.npm.config.clearCredentialsByURI(reg) 41 42 await this.npm.config.save('user') 43 } 44} 45module.exports = Logout 46