-
Notifications
You must be signed in to change notification settings - Fork 903
/
logout.ts
103 lines (88 loc) · 2.91 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Command } from "../command";
import { logger } from "../logger";
import * as clc from "colorette";
import * as utils from "../utils";
import { promptOnce } from "../prompt";
import { Options } from "../options";
import { Account } from "../types/auth";
import {
getAllAccounts,
getGlobalDefaultAccount,
getAdditionalAccounts,
setRefreshToken,
logout,
setGlobalDefaultAccount,
} from "../auth";
export const command = new Command("logout [email]")
.description("log the CLI out of Firebase")
.action(logoutAction);
/**
* Logout command action
* @param email Email of account to log out.
* @param options Command options.
*/
export async function logoutAction(email: string | undefined, options: Options): Promise<void> {
const globalToken = utils.getInheritedOption(options, "token");
utils.assertIsStringOrUndefined(globalToken);
const allAccounts = getAllAccounts();
if (allAccounts.length === 0 && !globalToken) {
logger.info("No need to logout, not logged in");
return;
}
const defaultAccount = getGlobalDefaultAccount();
const additionalAccounts = getAdditionalAccounts();
const accountsToLogOut = email ? allAccounts.filter((a) => a.user.email === email) : allAccounts;
if (email && accountsToLogOut.length === 0) {
utils.logWarning(`No account matches ${email}, can't log out.`);
return;
}
// If they are logging out of their primary account, choose one to
// replace it.
const logoutDefault = email === defaultAccount?.user.email;
let newDefaultAccount: Account | undefined = undefined;
if (logoutDefault && additionalAccounts.length > 0) {
if (additionalAccounts.length === 1) {
newDefaultAccount = additionalAccounts[0];
} else {
const choices = additionalAccounts.map((a) => {
return {
name: a.user.email,
value: a,
};
});
newDefaultAccount = await promptOnce({
type: "list",
message:
"You are logging out of your default account, which account should become the new default?",
choices,
});
}
}
for (const account of accountsToLogOut) {
const token = account.tokens.refresh_token;
if (token) {
setRefreshToken(token);
try {
await logout(token);
} catch (e: any) {
utils.logWarning(
`Invalid refresh token for ${account.user.email}, did not need to deauthorize`,
);
}
utils.logSuccess(`Logged out from ${clc.bold(account.user.email)}`);
}
}
if (globalToken) {
setRefreshToken(globalToken);
try {
await logout(globalToken);
} catch (e: any) {
utils.logWarning("Invalid refresh token, did not need to deauthorize");
}
utils.logSuccess(`Logged out from token "${clc.bold(globalToken)}"`);
}
if (newDefaultAccount) {
utils.logSuccess(`Setting default account to "${newDefaultAccount.user.email}"`);
setGlobalDefaultAccount(newDefaultAccount);
}
}