-
Notifications
You must be signed in to change notification settings - Fork 903
/
login-use.ts
63 lines (55 loc) · 2.3 KB
/
login-use.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
import * as clc from "colorette";
import { Command } from "../command";
import * as utils from "../utils";
import * as auth from "../auth";
import { FirebaseError } from "../error";
export const command = new Command("login:use <email>")
.description(
"set the default account to use for this project directory or the global default account if not in a Firebase project directory",
)
.action((email: string, options: any) => {
const allAccounts = auth.getAllAccounts();
const accountExists = allAccounts.some((a) => a.user.email === email);
if (!accountExists) {
throw new FirebaseError(
`Account ${email} does not exist, run "${clc.bold(
"firebase login:list",
)}" to see valid accounts`,
);
}
const projectDir = options.projectRoot as string | null;
// if current directory is a Firebase project directory, set the default account for this project directory
// otherwise, set the global default account
if (projectDir) {
if (options.user.email === email) {
throw new FirebaseError(`Already using account ${email} for this project directory.`);
}
auth.setProjectAccount(projectDir, email);
utils.logSuccess(`Set default account ${email} for current project directory.`);
return email;
} else {
if (options.user.email === email) {
throw new FirebaseError(`Already using account ${email} for the global default account.`);
}
const newDefaultAccount = allAccounts.find((a) => a.user.email === email);
if (!newDefaultAccount) {
// should never happen
throw new FirebaseError(
`Account ${email} does not exist, run "${clc.bold(
"firebase login:list",
)}" to see valid accounts`,
);
}
const oldDefaultAccount = auth.getGlobalDefaultAccount();
if (!oldDefaultAccount) {
// should never happen
throw new FirebaseError("Could not determine global default account");
}
// set new default account and removes it from additional accounts
auth.setGlobalDefaultAccount(newDefaultAccount);
// add old default account as additional account
auth.addAdditionalAccount(oldDefaultAccount);
utils.logSuccess(`Set global default account to ${email}.`);
return email;
}
});