Skip to content

Commit

Permalink
logout always updates local config
Browse files Browse the repository at this point in the history
fixes #166
  • Loading branch information
tbeseda committed Jan 23, 2024
1 parent a373c88 commit 2b66092
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/commands/logout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function action (params) {
return Error('Config file not found, cannot log out of Begin session')
}

let config = JSON.parse(readFileSync(configPath))
let config = JSON.parse(readFileSync(configPath).toString())
let { access_token, stagingAPI } = config
let { __BEGIN_TEST_URL__ } = process.env
let domain = __BEGIN_TEST_URL__
Expand All @@ -41,23 +41,31 @@ async function action (params) {

let tiny = require('tiny-json-http')
let url = domain + `/clients/${clientID}/token/delete`
let message = ''
try {
let authorization = `Bearer ${access_token}`
await tiny.post({
url,
headers: { authorization },
body: { access_token },
})

}
catch (err) {
if (err?.body?.errors?.includes('token_invalid_or_expired'))
message = 'Could not revoke access token, it may have already expired.'
else
// err can be vague here; try to get a more specific message
message = err?.body?.error || err?.body?.errors || err?.message || err || 'Unknown error'
message += '\n'
}
finally {
let now = new Date().toISOString()
config.modified = now
delete config.access_token
delete config.device_code
writeFile(configPath, JSON.stringify(config, null, 2))
return 'Successfully logged out!'
}
catch (err) {
if (err?.body?.errors?.includes('token_invalid_or_expired')) return Error('Invalid client ID or access token')
return err
message += 'Successfully logged out!'
}

return message
}

0 comments on commit 2b66092

Please sign in to comment.