1const npmFetch = require('npm-registry-fetch') 2const { getAuth } = npmFetch 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 const level = this.npm.config.find(`${auth.regKey}:${auth.authKey}`) 23 24 // find the config level and only delete from there 25 if (auth.token) { 26 log.verbose('logout', `clearing token for ${reg}`) 27 await npmFetch(`/-/user/token/${encodeURIComponent(auth.token)}`, { 28 ...this.npm.flatOptions, 29 registry: reg, 30 method: 'DELETE', 31 ignoreBody: true, 32 }) 33 } else if (auth.isBasicAuth) { 34 log.verbose('logout', `clearing user credentials for ${reg}`) 35 } else { 36 const msg = `not logged in to ${reg}, so can't log out!` 37 throw Object.assign(new Error(msg), { code: 'ENEEDAUTH' }) 38 } 39 40 if (scope) { 41 this.npm.config.delete(regRef, level) 42 } 43 44 this.npm.config.clearCredentialsByURI(reg, level) 45 46 await this.npm.config.save(level) 47 } 48} 49module.exports = Logout 50