1
1
const inquirer = require("inquirer");
2
2
const { Command } = require("commander");
3
3
const Client = require("../client");
4
- const { sdkForConsole, questionGetEndpoint } = require("../sdks");
4
+ const { sdkForConsole } = require("../sdks");
5
5
const { globalConfig, localConfig } = require("../config");
6
6
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
7
7
const ID = require("../id");
8
8
{% if sdk .test != " true" %}
9
- const { questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10
- const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
9
+ const { questionsLogin, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10
+ const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
11
11
12
12
const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1';
13
13
14
- const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => {
15
- const oldCurrent = globalConfig.getCurrentLogin();
16
- let answers = {};
17
- let configEndpoint = DEFAULT_ENDPOINT;
18
-
19
- if (selfHosted) {
20
- answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionLoginWithEndpoint);
21
- configEndpoint = answers.endpoint;
22
- } else {
23
- answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
24
- }
14
+ const loginCommand = async ({ email, password, endpoint, mfa, code }) => {
15
+ const oldCurrent = globalConfig.getCurrentSession();
16
+ let configEndpoint = endpoint ?? DEFAULT_ENDPOINT;
25
17
18
+ const answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
26
19
27
20
if (answers.method === 'select') {
28
21
const accountId = answers.accountId;
29
22
30
- if (!globalConfig.getLoginIds ().includes(accountId)) {
31
- throw Error('Login ID not found');
23
+ if (!globalConfig.getSessionIds ().includes(accountId)) {
24
+ throw Error('Session ID not found');
32
25
}
33
26
34
- globalConfig.setCurrentLogin (accountId);
27
+ globalConfig.setCurrentSession (accountId);
35
28
success(`Current account is ${accountId}`);
36
29
37
30
return;
38
31
}
39
32
40
33
const id = ID.unique();
41
34
42
- globalConfig.addLogin (id, {});
43
- globalConfig.setCurrentLogin (id);
35
+ globalConfig.addSession (id, {});
36
+ globalConfig.setCurrentSession (id);
44
37
globalConfig.setEndpoint(configEndpoint);
45
38
globalConfig.setEmail(answers.email);
46
39
@@ -86,8 +79,8 @@ const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }
86
79
parseOutput: false
87
80
});
88
81
} else {
89
- globalConfig.removeLogin (id);
90
- globalConfig.setCurrentLogin (oldCurrent);
82
+ globalConfig.removeSession (id);
83
+ globalConfig.setCurrentSession (oldCurrent);
91
84
throw error;
92
85
}
93
86
}
@@ -123,7 +116,8 @@ const whoami = new Command("whoami")
123
116
'ID': account.$id,
124
117
'Name': account.name,
125
118
'Email': account.email,
126
- 'MFA enabled': account.mfa ? 'Yes' : 'No'
119
+ 'MFA enabled': account.mfa ? 'Yes' : 'No',
120
+ 'Endpoint': globalConfig.getEndpoint()
127
121
}
128
122
];
129
123
if (json) {
@@ -137,7 +131,6 @@ const whoami = new Command("whoami")
137
131
138
132
const login = new Command("login")
139
133
.description(commandDescriptions['login'])
140
- .option(`-sh, --self-hosted`, `Flag for enabling custom endpoint for self hosted instances`)
141
134
.option(`--email [email]`, `User email`)
142
135
.option(`--password [password]`, `User password`)
143
136
.option(`--endpoint [endpoint]`, `Appwrite endpoint for self hosted instances`)
@@ -148,7 +141,7 @@ const login = new Command("login")
148
141
})
149
142
.action(actionRunner(loginCommand));
150
143
151
- const singleLogout = async (accountId) => {
144
+ const deleteSession = async (accountId) => {
152
145
try {
153
146
let client = await sdkForConsole();
154
147
@@ -158,11 +151,11 @@ const singleLogout = async (accountId) => {
158
151
sdk: client
159
152
})
160
153
161
- globalConfig.removeLogin (accountId);
154
+ globalConfig.removeSession (accountId);
162
155
} catch (e) {
163
156
error('Unable to log out, removing locally saved session information')
164
157
}
165
- globalConfig.removeLogin (accountId);
158
+ globalConfig.removeSession (accountId);
166
159
}
167
160
168
161
const logout = new Command("logout")
@@ -171,14 +164,14 @@ const logout = new Command("logout")
171
164
helpWidth: process.stdout.columns || 80
172
165
})
173
166
.action(actionRunner(async () => {
174
- const logins = globalConfig.getLogins ();
175
- const current = globalConfig.getCurrentLogin ();
167
+ const sessions = globalConfig.getSessions ();
168
+ const current = globalConfig.getCurrentSession ();
176
169
177
170
if (current === '') {
178
171
return;
179
172
}
180
- if (logins .length === 1) {
181
- await singleLogout (current);
173
+ if (sessions .length === 1) {
174
+ await deleteSession (current);
182
175
success();
183
176
184
177
return;
@@ -188,16 +181,16 @@ const logout = new Command("logout")
188
181
189
182
if (answers.accounts) {
190
183
for (let accountId of answers.accounts) {
191
- globalConfig.setCurrentLogin (accountId);
192
- await singleLogout (accountId);
184
+ globalConfig.setCurrentSession (accountId);
185
+ await deleteSession (accountId);
193
186
}
194
187
}
195
188
196
- const leftLogins = globalConfig.getLogins ();
189
+ const remainingSessions = globalConfig.getSessions ();
197
190
198
- if (leftLogins .length > 0 && leftLogins .filter(login => login .id === current).length !== 1) {
199
- const accountId = leftLogins [0].id;
200
- globalConfig.setCurrentLogin (accountId);
191
+ if (remainingSessions .length > 0 && remainingSessions .filter(session => session .id === current).length !== 1) {
192
+ const accountId = remainingSessions [0].id;
193
+ globalConfig.setCurrentSession (accountId);
201
194
202
195
success(`Current account is ${accountId}`);
203
196
}
@@ -250,8 +243,8 @@ const client = new Command("client")
250
243
if (!response.version) {
251
244
throw new Error();
252
245
}
253
- globalConfig.setCurrentLogin (id);
254
- globalConfig.addLogin (id, {});
246
+ globalConfig.setCurrentSession (id);
247
+ globalConfig.addSession (id, {});
255
248
globalConfig.setEndpoint(endpoint);
256
249
} catch (_) {
257
250
throw new Error("Invalid endpoint or your Appwrite server is not running as expected.");
@@ -271,11 +264,11 @@ const client = new Command("client")
271
264
}
272
265
273
266
if (reset !== undefined) {
274
- const logins = globalConfig.getLogins ();
267
+ const sessions = globalConfig.getSessions ();
275
268
276
- for (let accountId of logins .map(login => login .id)) {
277
- globalConfig.setCurrentLogin (accountId);
278
- await singleLogout (accountId);
269
+ for (let accountId of sessions .map(session => session .id)) {
270
+ globalConfig.setCurrentSession (accountId);
271
+ await deleteSession (accountId);
279
272
}
280
273
}
281
274
@@ -297,8 +290,8 @@ const migrate = async () => {
297
290
email: 'legacy'
298
291
};
299
292
300
- globalConfig.addLogin (id, data);
301
- globalConfig.setCurrentLogin (id);
293
+ globalConfig.addSession (id, data);
294
+ globalConfig.setCurrentSession (id);
302
295
globalConfig.delete('endpoint');
303
296
globalConfig.delete('cookie');
304
297
0 commit comments