| 12 | ] |
| 13 | |
| 14 | async exec () { |
| 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 | } |
| 49 | |
| 50 | module.exports = Logout |