Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat adding register #872

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion templates/cli/index.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -62,6 +62,7 @@ program
{% if sdk.test != "true" %}
.addCommand(whoami)
.addCommand(login)
.addCommand(register)
.addCommand(init)
.addCommand(pull)
.addCommand(push)
Expand Down
47 changes: 44 additions & 3 deletions templates/cli/lib/commands/generic.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,50 @@ 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 = {};
let configEndpoint = DEFAULT_ENDPOINT;

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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -308,6 +348,7 @@ module.exports = {
loginCommand,
whoami,
login,
register,
logout,
{% endif %}
migrate,
Expand Down
48 changes: 47 additions & 1 deletion templates/cli/lib/questions.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -800,5 +844,7 @@ module.exports = {
questionsListFactors,
questionsMfaChallenge,
questionGetEndpoint,
questionLoginWithEndpoint
questionLoginWithEndpoint,
questionsRegister,
questionsRegisterWithEndpoint
};
Loading