From 0b817a734e4fbc3003fe6830d3f5a75ba171d9f1 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Thu, 6 Jun 2024 22:25:53 -0400 Subject: [PATCH] feat: Added Register --- templates/cli/index.js.twig | 3 +- templates/cli/lib/commands/generic.js.twig | 47 +++++++++++++++++++-- templates/cli/lib/questions.js.twig | 48 +++++++++++++++++++++- 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/templates/cli/index.js.twig b/templates/cli/index.js.twig index 6aa486cd7..9cb26842c 100644 --- a/templates/cli/index.js.twig +++ b/templates/cli/index.js.twig @@ -12,7 +12,7 @@ const { commandDescriptions, cliConfig } = require("./lib/parser"); const { client } = require("./lib/commands/generic"); const inquirer = require("inquirer"); {% if sdk.test != "true" %} -const { login, logout, whoami, migrate } = require("./lib/commands/generic"); +const { login, register, logout, whoami, migrate } = require("./lib/commands/generic"); const { init } = require("./lib/commands/init"); const { pull } = require("./lib/commands/pull"); const { push } = require("./lib/commands/push"); @@ -62,6 +62,7 @@ program {% if sdk.test != "true" %} .addCommand(whoami) .addCommand(login) + .addCommand(register) .addCommand(init) .addCommand(pull) .addCommand(push) diff --git a/templates/cli/lib/commands/generic.js.twig b/templates/cli/lib/commands/generic.js.twig index cd3332adf..443c69464 100644 --- a/templates/cli/lib/commands/generic.js.twig +++ b/templates/cli/lib/commands/generic.js.twig @@ -6,11 +6,42 @@ const { globalConfig, localConfig } = require("../config"); const { actionRunner, success, parseBool, commandDescriptions, error, parse, log, drawTable } = require("../parser"); const ID = require("../id"); {% if sdk.test != "true" %} -const { questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions"); -const { accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account"); +const { questionsRegister, questionsRegisterWithEndpoint, questionsLogin, questionLoginWithEndpoint, questionsLogout, questionsListFactors, questionsMfaChallenge } = require("../questions"); +const { accountCreate, accountUpdateMfaChallenge, accountCreateMfaChallenge, accountGet, accountCreateEmailPasswordSession, accountDeleteSession } = require("./account"); const DEFAULT_ENDPOINT = 'https://cloud.appwrite.io/v1'; +const registerCommand = async ({ selfHosted, email, password, endpoint, name }) => { + let answers; + let configEndpoint = DEFAULT_ENDPOINT; + + if (selfHosted) { + answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionsRegisterWithEndpoint); + configEndpoint = answers.endpoint; + } else { + answers = email && password ? { email, password } : await inquirer.prompt(questionsRegister); + } + + globalConfig.setEndpoint(configEndpoint); + + let client = await sdkForConsole(false); + + try { + await accountCreate({ + userId: ID.unique(), + email: answers.email, + password: answers.password, + parseOutput: false, + name: answers.name, + sdk: client, + }) + + success(); + } catch (e) { + throw e; + } + +} const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code }) => { const oldCurrent = globalConfig.getCurrentLogin(); let answers = {}; @@ -18,7 +49,7 @@ const loginCommand = async ({ selfHosted, email, password, endpoint, mfa, code } if (selfHosted) { answers = endpoint && email && password ? { endpoint, email, password } : await inquirer.prompt(questionLoginWithEndpoint); - configEndpoint = answers.endpoint; + configEndpoint = answers.endpoint; } else { answers = email && password ? { email, password } : await inquirer.prompt(questionsLogin); } @@ -148,6 +179,15 @@ const login = new Command("login") }) .action(actionRunner(loginCommand)); +const register = new Command("register") + .description(commandDescriptions['login']) + .option(`-sh, --self-hosted`, `Flag for enabling custom endpoint for self hosted instances`) + .option(`--name [name]`, `User name`) + .option(`--email [email]`, `User email`) + .option(`--password [password]`, `User password`) + .option(`--endpoint [endpoint]`, `Appwrite endpoint for self hosted instances`) + .action(actionRunner(registerCommand)); + const singleLogout = async (accountId) => { try { let client = await sdkForConsole(); @@ -308,6 +348,7 @@ module.exports = { loginCommand, whoami, login, + register, logout, {% endif %} migrate, diff --git a/templates/cli/lib/questions.js.twig b/templates/cli/lib/questions.js.twig index 7bd8c965b..6d0319789 100644 --- a/templates/cli/lib/questions.js.twig +++ b/templates/cli/lib/questions.js.twig @@ -531,6 +531,50 @@ const questionLoginWithEndpoint = [ questionsLogin[3] ] +const questionsRegister = [ + { + type: "input", + name: "name", + message: "Enter your name", + validate(value) { + if (!value) { + return "Please enter your name"; + } + return true; + }, + }, + { + type: "input", + name: "email", + message: "Enter your email", + validate(value) { + if (!value) { + return "Please enter your email"; + } + return true; + }, + }, + { + type: "password", + name: "password", + message: "Enter your password", + mask: "*", + validate(value) { + if (!value) { + return "Please enter your password"; + } + return true; + }, + }, +]; + +const questionsRegisterWithEndpoint = [ + questionGetEndpoint[0], + questionsRegister[0], + questionsRegister[1], + questionsRegister[2] +] + const questionsLogout = [ { type: "checkbox", @@ -800,5 +844,7 @@ module.exports = { questionsListFactors, questionsMfaChallenge, questionGetEndpoint, - questionLoginWithEndpoint + questionLoginWithEndpoint, + questionsRegister, + questionsRegisterWithEndpoint };