Skip to content

Commit 144e043

Browse files
committed
feat(cli): Interactive multiple user logout
1 parent d250003 commit 144e043

File tree

2 files changed

+98
-9
lines changed

2 files changed

+98
-9
lines changed

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

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { sdkForConsole, questionGetEndpoint } = require("../sdks");
55
const { globalConfig, localConfig } = require("../config");
66
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
77
{% if sdk.test != "true" %}
8-
const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
8+
const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
99
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1010
const ID = require("../id");
1111

@@ -165,12 +165,8 @@ login
165165

166166
}));
167167

168-
const logout = new Command("logout")
169-
.description(commandDescriptions['logout'])
170-
.configureHelp({
171-
helpWidth: process.stdout.columns || 80
172-
})
173-
.action(actionRunner(async () => {
168+
const singleLogout = async (accountId) => {
169+
try {
174170
let client = await sdkForConsole();
175171

176172
await accountDeleteSession({
@@ -179,8 +175,58 @@ const logout = new Command("logout")
179175
sdk: client
180176
})
181177

182-
globalConfig.setCookie("");
183-
success()
178+
globalConfig.removeLogin(accountId);
179+
} catch (e) {
180+
error('Unable to log out, removing locally saved session information')
181+
}
182+
globalConfig.removeLogin(accountId);
183+
}
184+
185+
const logout = new Command("logout")
186+
.description(commandDescriptions['logout'])
187+
.configureHelp({
188+
helpWidth: process.stdout.columns || 80
189+
})
190+
.action(actionRunner(async () => {
191+
const logins = globalConfig.getLogins();
192+
const current = globalConfig.getCurrentLogin();
193+
194+
if (current === '') {
195+
return;
196+
}
197+
if (logins.length === 1) {
198+
await singleLogout(current);
199+
success();
200+
201+
return;
202+
}
203+
204+
const answers = await inquirer.prompt(questionsLogout);
205+
const accountIds = [];
206+
207+
if (answers.method === 'all') {
208+
accountIds.push(...logins.map(login => login.id));
209+
}
210+
211+
if (answers.method === 'selected' && answers.accounts) {
212+
accountIds.push(...answers.accounts);
213+
}
214+
215+
for (let accountId of accountIds) {
216+
globalConfig.setCurrentLogin(accountId);
217+
await singleLogout(accountId);
218+
}
219+
220+
const leftLogins = globalConfig.getLogins();
221+
222+
if (leftLogins.length > 0 && leftLogins.filter(login => login.id === current).length !== 1) {
223+
const accountId = leftLogins[0].id;
224+
globalConfig.setCurrentLogin(accountId);
225+
226+
success(`Current account is ${accountId}`);
227+
}
228+
229+
success();
184230
}));
185231
{% endif %}
186232

templates/cli/lib/questions.js.twig

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,48 @@ const questionsLogin = [
342342

343343
];
344344

345+
const questionsLogout = [
346+
{
347+
type: "list",
348+
name: "method",
349+
message: "Which account would you like to logout from?",
350+
choices() {
351+
const logins = globalConfig.getLogins();
352+
353+
return [
354+
{ name: 'Account in use' },
355+
{ name: 'Selected accounts', value: 'selected' },
356+
{ name: 'All accounts', value: 'all' }
357+
];
358+
},
359+
when: () => globalConfig.getCurrentLogin() !== ''
360+
},
361+
{
362+
type: "checkbox",
363+
name: "accounts",
364+
message: "Select accounts to logout from",
365+
when: (answers) => answers.method === 'selected',
366+
validate: (value) => validateRequired('account', value),
367+
choices() {
368+
const logins = globalConfig.getLogins();
369+
const current = globalConfig.getCurrentLogin();
370+
371+
const data = [];
372+
373+
const longestEmail = logins.reduce((prev, current) => (prev && prev.email > current.email) ? prev : current).email.length;
374+
375+
logins.forEach((login) => {
376+
data.push({
377+
value: login.id,
378+
name: `${login.email.padEnd(longestEmail)} ${current === login.id ? chalk.green.bold('In use') : ' '.repeat(6)} ${login.endpoint}`,
379+
});
380+
})
381+
382+
return data;
383+
}
384+
}
385+
];
386+
345387
const questionsPushResources = [
346388
{
347389
type: "checkbox",
@@ -580,6 +622,7 @@ const questionGetEndpoint = [
580622
module.exports = {
581623
questionsPullProject,
582624
questionsLogin,
625+
questionsLogout,
583626
questionsPullFunction,
584627
questionsPullCollection,
585628
questionsPushResources,

0 commit comments

Comments
 (0)