Skip to content

Commit 0b817a7

Browse files
committed
feat: Added Register
1 parent 2fd8f2b commit 0b817a7

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

templates/cli/index.js.twig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { commandDescriptions, cliConfig } = require("./lib/parser");
1212
const { client } = require("./lib/commands/generic");
1313
const inquirer = require("inquirer");
1414
{% if sdk.test != "true" %}
15-
const { login, logout, whoami, migrate } = require("./lib/commands/generic");
15+
const { login, register, logout, whoami, migrate } = require("./lib/commands/generic");
1616
const { init } = require("./lib/commands/init");
1717
const { pull } = require("./lib/commands/pull");
1818
const { push } = require("./lib/commands/push");
@@ -62,6 +62,7 @@ program
6262
{% if sdk.test != "true" %}
6363
.addCommand(whoami)
6464
.addCommand(login)
65+
.addCommand(register)
6566
.addCommand(init)
6667
.addCommand(pull)
6768
.addCommand(push)

templates/cli/lib/commands/generic.js.twig

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,50 @@ const { globalConfig, localConfig } = require("../config");
66
const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser");
77
const ID = require("../id");
88
{% if sdk.test != "true" %}
9-
const { questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10-
const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
9+
const { questionsRegister, questionsRegisterWithEndpoint, questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions");
10+
const { accountCreate, accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account");
1111

1212
const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1';
1313

14+
const registerCommand = async ({ selfHosted, email, password, endpoint, name }) => {
15+
let answers;
16+
let configEndpoint = DEFAULT_ENDPOINT;
17+
18+
if (selfHosted) {
19+
answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionsRegisterWithEndpoint);
20+
configEndpoint = answers.endpoint;
21+
} else {
22+
answers = email && password ? { email, password } : await inquirer.prompt(questionsRegister);
23+
}
24+
25+
globalConfig.setEndpoint(configEndpoint);
26+
27+
let client = await sdkForConsole(false);
28+
29+
try {
30+
await accountCreate({
31+
userId: ID.unique(),
32+
email: answers.email,
33+
password: answers.password,
34+
parseOutput: false,
35+
name: answers.name,
36+
sdk: client,
37+
})
38+
39+
success();
40+
} catch (e) {
41+
throw e;
42+
}
43+
44+
}
1445
const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => {
1546
const oldCurrent = globalConfig.getCurrentLogin();
1647
let answers = {};
1748
let configEndpoint = DEFAULT_ENDPOINT;
1849

1950
if (selfHosted) {
2051
answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionLoginWithEndpoint);
21-
configEndpoint = answers.endpoint;
52+
configEndpoint = answers.endpoint;
2253
} else {
2354
answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin);
2455
}
@@ -148,6 +179,15 @@ const login = new Command("login")
148179
})
149180
.action(actionRunner(loginCommand));
150181

182+
const register = new Command("register")
183+
.description(commandDescriptions['login'])
184+
.option(`-sh, --self-hosted`, `Flag for enabling custom endpoint for self hosted instances`)
185+
.option(`--name [name]`, `User name`)
186+
.option(`--email [email]`, `User email`)
187+
.option(`--password [password]`, `User password`)
188+
.option(`--endpoint [endpoint]`, `Appwrite endpoint for self hosted instances`)
189+
.action(actionRunner(registerCommand));
190+
151191
const singleLogout = async (accountId) => {
152192
try {
153193
let client = await sdkForConsole();
@@ -308,6 +348,7 @@ module.exports = {
308348
loginCommand,
309349
whoami,
310350
login,
351+
register,
311352
logout,
312353
{% endif %}
313354
migrate,

templates/cli/lib/questions.js.twig

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,50 @@ const questionLoginWithEndpoint = [
531531
questionsLogin[3]
532532
]
533533

534+
const questionsRegister = [
535+
{
536+
type: "input",
537+
name: "name",
538+
message: "Enter your name",
539+
validate(value) {
540+
if (!value) {
541+
return "Please enter your name";
542+
}
543+
return true;
544+
},
545+
},
546+
{
547+
type: "input",
548+
name: "email",
549+
message: "Enter your email",
550+
validate(value) {
551+
if (!value) {
552+
return "Please enter your email";
553+
}
554+
return true;
555+
},
556+
},
557+
{
558+
type: "password",
559+
name: "password",
560+
message: "Enter your password",
561+
mask: "*",
562+
validate(value) {
563+
if (!value) {
564+
return "Please enter your password";
565+
}
566+
return true;
567+
},
568+
},
569+
];
570+
571+
const questionsRegisterWithEndpoint = [
572+
questionGetEndpoint[0],
573+
questionsRegister[0],
574+
questionsRegister[1],
575+
questionsRegister[2]
576+
]
577+
534578
const questionsLogout = [
535579
{
536580
type: "checkbox",
@@ -800,5 +844,7 @@ module.exports = {
800844
questionsListFactors,
801845
questionsMfaChallenge,
802846
questionGetEndpoint,
803-
questionLoginWithEndpoint
847+
questionLoginWithEndpoint,
848+
questionsRegister,
849+
questionsRegisterWithEndpoint
804850
};

0 commit comments

Comments
 (0)