|
1 | 1 | const inquirer = require("inquirer");
|
2 | 2 | const { Command } = require("commander");
|
3 | 3 | const Client = require("../client");
|
4 |
| -const { sdkForConsole } = require("../sdks"); |
| 4 | +const { sdkForConsole, questionGetEndpoint } = require("../sdks"); |
5 | 5 | const { globalConfig, localConfig } = require("../config");
|
6 | 6 | const { actionRunner, success, parseBool, commandDescriptions, error, parse, drawTable } = require("../parser");
|
7 | 7 | {% if sdk.test != "true" %}
|
8 | 8 | const { questionsLogin, questionsListFactors, questionsMfaChallenge } = require("../questions");
|
9 | 9 | const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
|
| 10 | +const ID = require("../id"); |
| 11 | + |
| 12 | +const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1'; |
| 13 | + |
| 14 | +const loginCommand = async ({ selfHosted }) => { |
| 15 | + const answers = await inquirer.prompt(questionsLogin); |
| 16 | + const oldCurrent = globalConfig.getCurrentLogin(); |
| 17 | + const id = ID.unique(); |
| 18 | + |
| 19 | + globalConfig.setCurrentLogin(id); |
| 20 | + globalConfig.addLogin(id, {}); |
| 21 | + globalConfig.setEmail(answers.email); |
| 22 | + globalConfig.setEndpoint(DEFAULT_ENDPOINT); |
| 23 | + |
| 24 | + if (selfHosted) { |
| 25 | + const selfHostedAnswers = await inquirer.prompt(questionGetEndpoint); |
| 26 | + |
| 27 | + globalConfig.setEndpoint(selfHostedAnswers.endpoint); |
| 28 | + } |
| 29 | + |
| 30 | + let client = await sdkForConsole(false); |
| 31 | + |
| 32 | + let account; |
| 33 | + |
| 34 | + try { |
| 35 | + await accountCreateEmailPasswordSession({ |
| 36 | + email: answers.email, |
| 37 | + password: answers.password, |
| 38 | + parseOutput: false, |
| 39 | + sdk: client |
| 40 | + }) |
| 41 | + |
| 42 | + client.setCookie(globalConfig.getCookie()); |
| 43 | + |
| 44 | + account = await accountGet({ |
| 45 | + sdk: client, |
| 46 | + parseOutput: false |
| 47 | + }); |
| 48 | + } catch (error) { |
| 49 | + if (error.response === 'user_more_factors_required') { |
| 50 | + const { factor } = await inquirer.prompt(questionsListFactors); |
| 51 | + |
| 52 | + const challenge = await accountCreateMfaChallenge({ |
| 53 | + factor, |
| 54 | + parseOutput: false, |
| 55 | + sdk: client |
| 56 | + }); |
| 57 | + |
| 58 | + const { otp } = await inquirer.prompt(questionsMfaChallenge); |
| 59 | + |
| 60 | + await accountUpdateMfaChallenge({ |
| 61 | + challengeId: challenge.$id, |
| 62 | + otp, |
| 63 | + parseOutput: false, |
| 64 | + sdk: client |
| 65 | + }); |
| 66 | + |
| 67 | + account = await accountGet({ |
| 68 | + sdk: client, |
| 69 | + parseOutput: false |
| 70 | + }); |
| 71 | + } else { |
| 72 | + globalConfig.removeLogin(id); |
| 73 | + globalConfig.setCurrentLogin(oldCurrent); |
| 74 | + throw error; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + success("Signed in as user with ID: " + account.$id); |
| 79 | +}; |
10 | 80 |
|
11 | 81 | const whoami = new Command("whoami")
|
12 | 82 | .description(commandDescriptions['whoami'])
|
@@ -49,61 +119,74 @@ const whoami = new Command("whoami")
|
49 | 119 |
|
50 | 120 | const login = new Command("login")
|
51 | 121 | .description(commandDescriptions['login'])
|
| 122 | + .option(`-sa, --self-hosted`, `Flag for enabling custom endpoint for self hosted instances`) |
52 | 123 | .configureHelp({
|
53 | 124 | helpWidth: process.stdout.columns || 80
|
54 | 125 | })
|
| 126 | + .action(actionRunner(loginCommand)); |
| 127 | + |
| 128 | +login |
| 129 | + .command('list') |
| 130 | + .description("List available logged accounts.") |
55 | 131 | .action(actionRunner(async () => {
|
56 |
| - const answers = await inquirer.prompt(questionsLogin) |
| 132 | + const logins = globalConfig.getLogins(); |
| 133 | + const current = globalConfig.getCurrentLogin(); |
57 | 134 |
|
58 |
| - let client = await sdkForConsole(false); |
| 135 | + const data = [...logins.map((login => { |
| 136 | + return { |
| 137 | + 'Current': login.id === current ? '*' : '', |
| 138 | + 'ID': login.id, |
| 139 | + 'Endpoint': login.endpoint, |
| 140 | + 'Email': login.email |
| 141 | + }; |
| 142 | + }))]; |
59 | 143 |
|
60 |
| - await accountCreateEmailPasswordSession({ |
61 |
| - email: answers.email, |
62 |
| - password: answers.password, |
63 |
| - parseOutput: false, |
64 |
| - sdk: client |
65 |
| - }) |
| 144 | + drawTable(data); |
66 | 145 |
|
67 |
| - client.setCookie(globalConfig.getCookie()); |
| 146 | + })); |
68 | 147 |
|
69 |
| - let account; |
| 148 | +login |
| 149 | + .command('change') |
| 150 | + .description("Change the current account") |
| 151 | + .option(`-a, --accountId <accountId>`, `Login ID`) |
| 152 | + .action(actionRunner(async ({ accountId }) => { |
| 153 | + const loginIds = globalConfig.getLoginIds(); |
70 | 154 |
|
71 |
| - try { |
72 |
| - account = await accountGet({ |
73 |
| - sdk: client, |
74 |
| - parseOutput: false |
75 |
| - }); |
76 |
| - } catch (error) { |
77 |
| - if (error.response === 'user_more_factors_required') { |
78 |
| - const { factor } = await inquirer.prompt(questionsListFactors); |
79 |
| - |
80 |
| - const challenge = await accountCreateMfaChallenge({ |
81 |
| - factor, |
82 |
| - parseOutput: false, |
83 |
| - sdk: client |
84 |
| - }); |
85 |
| - |
86 |
| - const { otp } = await inquirer.prompt(questionsMfaChallenge); |
87 |
| - |
88 |
| - await accountUpdateMfaChallenge({ |
89 |
| - challengeId: challenge.$id, |
90 |
| - otp, |
91 |
| - parseOutput: false, |
92 |
| - sdk: client |
93 |
| - }); |
94 |
| - |
95 |
| - account = await accountGet({ |
96 |
| - sdk: client, |
97 |
| - parseOutput: false |
98 |
| - }); |
99 |
| - } else { |
100 |
| - throw error; |
101 |
| - } |
| 155 | + if (!loginIds.includes(accountId)) { |
| 156 | + throw Error('Login ID not found'); |
102 | 157 | }
|
103 | 158 |
|
104 |
| - success("Signed in as user with ID: " + account.$id); |
| 159 | + globalConfig.setCurrentLogin(accountId); |
| 160 | + success(`Current account is ${accountId}`); |
105 | 161 | }));
|
106 | 162 |
|
| 163 | +login |
| 164 | + .command('migrate') |
| 165 | + .description("Migrate existing login to new scheme") |
| 166 | + .action(actionRunner(async ({ accountId }) => { |
| 167 | + const endpoint = globalConfig.getEndpoint(); |
| 168 | + const cookie = globalConfig.getCookie(); |
| 169 | + |
| 170 | + if (endpoint === '' || cookie === '') { |
| 171 | + throw Error(`Couldn't find any existing account credentials`) |
| 172 | + } |
| 173 | + |
| 174 | + const id = ID.unique(); |
| 175 | + const data = { |
| 176 | + endpoint, |
| 177 | + cookie, |
| 178 | + email: 'legacy' |
| 179 | + }; |
| 180 | + |
| 181 | + globalConfig.addLogin(id, data); |
| 182 | + globalConfig.setCurrentLogin(id); |
| 183 | + globalConfig.delete('endpoint'); |
| 184 | + globalConfig.delete('cookie'); |
| 185 | + |
| 186 | + success(`Account was migrated and it's the current account`); |
| 187 | + })); |
| 188 | + |
| 189 | + |
107 | 190 | const logout = new Command("logout")
|
108 | 191 | .description(commandDescriptions['logout'])
|
109 | 192 | .configureHelp({
|
|
0 commit comments