-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlogout.ts
78 lines (72 loc) · 2.51 KB
/
logout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Command } from '@contentstack/cli-command';
import {
cliux,
configHandler,
printFlagDeprecation,
flags,
authHandler as oauthHandler,
managementSDKClient,
FlagInput,
formatError,
} from '@contentstack/cli-utilities';
import { authHandler } from '../../utils';
import { BaseCommand } from '../../base-command';
export default class LogoutCommand extends BaseCommand<typeof LogoutCommand> {
static run;
static description = 'User session logout';
static examples = ['$ csdx auth:logout', '$ csdx auth:logout -y', '$ csdx auth:logout --yes'];
static flags: FlagInput = {
yes: flags.boolean({
char: 'y',
description: 'Force log out by skipping the confirmation.',
required: false,
default: false,
}),
force: flags.boolean({
char: 'f',
description: 'Force log out by skipping the confirmation.',
required: false,
hidden: true,
default: false,
parse: printFlagDeprecation(['-f', '--force'], ['-y', '--yes']),
}),
};
static aliases = ['logout'];
async run(): Promise<any> {
const { flags: logoutFlags } = await this.parse(LogoutCommand);
let confirm = logoutFlags.force === true || logoutFlags.yes === true;
if (!confirm) {
confirm = await cliux.inquire({
type: 'confirm',
message: 'CLI_AUTH_LOGOUT_CONFIRM',
name: 'confirmation',
});
}
try {
const managementAPIClient = await managementSDKClient({ host: this.cmaHost, skipTokenValidity: true });
authHandler.client = managementAPIClient;
if (confirm === true && (await oauthHandler.isAuthenticated())) {
cliux.loader('CLI_AUTH_LOGOUT_LOADER_START');
if (await oauthHandler.isAuthorisationTypeBasic()) {
await authHandler.logout(configHandler.get('authtoken'));
} else if (await oauthHandler.isAuthorisationTypeOAuth()) {
await oauthHandler.oauthLogout();
}
cliux.loader('');
this.logger.info('successfully logged out');
cliux.success('CLI_AUTH_LOGOUT_SUCCESS');
} else {
cliux.success('CLI_AUTH_LOGOUT_ALREADY');
}
} catch (error) {
let errorMessage = formatError(error) || 'Something went wrong while logging out. Please try again.';
this.logger.error('Logout failed', errorMessage);
cliux.print('CLI_AUTH_LOGOUT_FAILED', { color: 'yellow' });
cliux.print(errorMessage, { color: 'red' });
} finally {
if (confirm === true) {
await oauthHandler.setConfigData('logout');
}
}
}
}