diff --git a/templates/cli/lib/commands/generic.js.twig b/templates/cli/lib/commands/generic.js.twig index 0c550de09..0ebb00798 100644 --- a/templates/cli/lib/commands/generic.js.twig +++ b/templates/cli/lib/commands/generic.js.twig @@ -172,12 +172,11 @@ const deleteSession = async (accountId) => { parseOutput: false, sdk: client }) - - globalConfig.removeSession(accountId); } catch (e) { error('Unable to log out, removing locally saved session information') + } finally { + globalConfig.removeSession(accountId); } - globalConfig.removeSession(accountId); } const logout = new Command("logout") @@ -195,6 +194,7 @@ const logout = new Command("logout") } if (sessions.length === 1) { await deleteSession(current); + globalConfig.setCurrentSession(''); success("Logging out"); return; @@ -216,6 +216,8 @@ const logout = new Command("logout") globalConfig.setCurrentSession(accountId); success(`Current account is ${accountId}`); + } else if (remainingSessions.length === 0) { + globalConfig.setCurrentSession(''); } success("Logging out"); diff --git a/templates/cli/lib/config.js.twig b/templates/cli/lib/config.js.twig index ca7607805..85b252879 100644 --- a/templates/cli/lib/config.js.twig +++ b/templates/cli/lib/config.js.twig @@ -681,15 +681,25 @@ class Global extends Config { getSessions() { const sessions = Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key)) + const current = this.getCurrentSession(); - return sessions.map((session) => { - - return { - id: session, - endpoint: this.data[session][Global.PREFERENCE_ENDPOINT], - email: this.data[session][Global.PREFERENCE_EMAIL] + const sessionMap = new Map(); + + sessions.forEach((sessionId) => { + const email = this.data[sessionId][Global.PREFERENCE_EMAIL]; + const endpoint = this.data[sessionId][Global.PREFERENCE_ENDPOINT]; + const key = `${email}|${endpoint}`; + + if (sessionId === current || !sessionMap.has(key)) { + sessionMap.set(key, { + id: sessionId, + endpoint: this.data[sessionId][Global.PREFERENCE_ENDPOINT], + email: this.data[sessionId][Global.PREFERENCE_EMAIL] + }); } - }) + }); + + return Array.from(sessionMap.values()); } addSession(session, data) {