Skip to content

Commit 85f51dc

Browse files
authored
Merge pull request #1220 from appwrite/fix-login-issues-cli
fix: some login issues in cli
2 parents a20b20c + 12e7d61 commit 85f51dc

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

templates/cli/lib/commands/generic.js.twig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,11 @@ const deleteSession = async (accountId) => {
172172
parseOutput: false,
173173
sdk: client
174174
})
175-
176-
globalConfig.removeSession(accountId);
177175
} catch (e) {
178176
error('Unable to log out, removing locally saved session information')
177+
} finally {
178+
globalConfig.removeSession(accountId);
179179
}
180-
globalConfig.removeSession(accountId);
181180
}
182181

183182
const logout = new Command("logout")
@@ -195,6 +194,7 @@ const logout = new Command("logout")
195194
}
196195
if (sessions.length === 1) {
197196
await deleteSession(current);
197+
globalConfig.setCurrentSession('');
198198
success("Logging out");
199199

200200
return;
@@ -216,6 +216,8 @@ const logout = new Command("logout")
216216
globalConfig.setCurrentSession(accountId);
217217

218218
success(`Current account is ${accountId}`);
219+
} else if (remainingSessions.length === 0) {
220+
globalConfig.setCurrentSession('');
219221
}
220222

221223
success("Logging out");

templates/cli/lib/config.js.twig

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,25 @@ class Global extends Config {
681681

682682
getSessions() {
683683
const sessions = Object.keys(this.data).filter((key) => !Global.IGNORE_ATTRIBUTES.includes(key))
684+
const current = this.getCurrentSession();
684685

685-
return sessions.map((session) => {
686-
687-
return {
688-
id: session,
689-
endpoint: this.data[session][Global.PREFERENCE_ENDPOINT],
690-
email: this.data[session][Global.PREFERENCE_EMAIL]
686+
const sessionMap = new Map();
687+
688+
sessions.forEach((sessionId) => {
689+
const email = this.data[sessionId][Global.PREFERENCE_EMAIL];
690+
const endpoint = this.data[sessionId][Global.PREFERENCE_ENDPOINT];
691+
const key = `${email}|${endpoint}`;
692+
693+
if (sessionId === current || !sessionMap.has(key)) {
694+
sessionMap.set(key, {
695+
id: sessionId,
696+
endpoint: this.data[sessionId][Global.PREFERENCE_ENDPOINT],
697+
email: this.data[sessionId][Global.PREFERENCE_EMAIL]
698+
});
691699
}
692-
})
700+
});
701+
702+
return Array.from(sessionMap.values());
693703
}
694704

695705
addSession(session, data) {

0 commit comments

Comments
 (0)