From aaccceb45f41476964968a910b8cc4c08e8f892f Mon Sep 17 00:00:00 2001 From: Joe Hand Date: Thu, 15 Dec 2016 16:27:54 -0800 Subject: [PATCH] add username and register/login prompts (#34) * add username and register/login prompts * email address -> email --- lib/commands/auth/login.js | 45 +++++++++++++++++++++--------- lib/commands/auth/register.js | 51 +++++++++++++++++++++++++--------- package.json | 1 + tests/auth.js | 52 +---------------------------------- 4 files changed, 72 insertions(+), 77 deletions(-) diff --git a/lib/commands/auth/login.js b/lib/commands/auth/login.js index 3733576..687c719 100644 --- a/lib/commands/auth/login.js +++ b/lib/commands/auth/login.js @@ -1,3 +1,4 @@ +var prompt = require('prompt') var ui = require('../../ui') var TownshipClient = require('../../township') @@ -8,19 +9,37 @@ module.exports = { } function login (opts) { - var email = opts.email - if (!email) ui.exitErr('Email is required.') - var password = opts.password - if (!password) ui.exitErr('Password is required.') + if (opts.email && opts.password) return makeRequest(opts) - var client = TownshipClient(opts) - - client.login({ - email: email, - password: password - }, function (err) { - if (err) ui.exitErr(err) - console.log('Logged in successfully.') - process.exit(0) + prompt.message = '' + prompt.colors = false + prompt.start() + prompt.get([{ + name: 'email', + description: 'Email', + required: true + }, + { + name: 'password', + description: 'Password', + required: true, + hidden: true, + replace: '*' + }], function (err, results) { + if (err) return console.log(err.message) + makeRequest(results) }) + + function makeRequest (user) { + var client = TownshipClient(opts) + + client.login({ + email: user.email, + password: user.password + }, function (err) { + if (err) ui.exitErr(err) + console.log('Logged in successfully.') + process.exit(0) + }) + } } diff --git a/lib/commands/auth/register.js b/lib/commands/auth/register.js index f59a17d..5646da7 100644 --- a/lib/commands/auth/register.js +++ b/lib/commands/auth/register.js @@ -1,3 +1,4 @@ +var prompt = require('prompt') var ui = require('../../ui') var TownshipClient = require('../../township') @@ -8,19 +9,43 @@ module.exports = { } function register (opts) { - var email = opts.email - if (!email) ui.exitErr(new Error('Email is required.')) - var password = opts.password - if (!password) ui.exitErr(new Error('Password is required.')) + if (opts.email && opts.username && opts.password) return makeRequest(opts) - var client = TownshipClient(opts) - - client.register({ - email: email, - password: password - }, function (err) { - if (err) ui.exitErr(err) - console.log('Registered successfully.') - process.exit(0) + prompt.message = '' + prompt.colors = false + prompt.start() + prompt.get([{ + name: 'email', + description: 'Email', + required: true + }, + { + name: 'username', + description: 'Username', + required: true + }, + { + name: 'password', + description: 'Password', + required: true, + hidden: true, + replace: '*' + }], function (err, results) { + if (err) return console.log(err.message) + makeRequest(results) }) + + function makeRequest (user) { + var client = TownshipClient(opts) + + client.register({ + email: user.email, + username: user.username, + password: user.password + }, function (err) { + if (err) ui.exitErr(err) + console.log('Registered successfully.') + process.exit(0) + }) + } } diff --git a/package.json b/package.json index 8edd5b1..b09a5d7 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "memdb": "^1.3.1", "pretty-bytes": "^4.0.2", "progress-string": "^1.2.1", + "prompt": "^1.0.0", "rimraf": "^2.5.4", "status-logger": "^3.0.0", "subcommand": "^2.0.4", diff --git a/tests/auth.js b/tests/auth.js index 21cd5ec..c4f81e9 100644 --- a/tests/auth.js +++ b/tests/auth.js @@ -27,34 +27,8 @@ authServer(port, function (server) { st.end() }) - test('auth - register requires email', function (t) { - var cmd = dat + ' register ' - var st = spawn(t, cmd, {cwd: baseTestDir}) - st.stderr.match(function (output) { - t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message') - st.kill() - return true - }) - st.stdout.empty() - st.fails() - st.end() - }) - - test('auth - register requires email and password', function (t) { - var cmd = dat + ' register --email=hello@bob.com' - var st = spawn(t, cmd, {cwd: baseTestDir}) - st.stderr.match(function (output) { - t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message') - st.kill() - return true - }) - st.stdout.empty() - st.fails() - st.end() - }) - test('auth - register works', function (t) { - var cmd = dat + ' register --email=hello@bob.com --password=joe' + var cmd = dat + ' register --email=hello@bob.com --password=joe --username=joe' var st = spawn(t, cmd, {cwd: baseTestDir}) st.stdout.match(function (output) { t.same(output.trim(), 'Registered successfully.', 'output success message') @@ -64,30 +38,6 @@ authServer(port, function (server) { st.end() }) - test('auth - login requires email', function (t) { - var cmd = dat + ' login --password=joe' - var st = spawn(t, cmd, {cwd: baseTestDir}) - st.stderr.match(function (output) { - t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message') - st.kill() - return true - }) - st.stdout.empty() - st.end() - }) - - test('auth - login requires email and password', function (t) { - var cmd = dat + ' login --email=hello@bob.com' - var st = spawn(t, cmd, {cwd: baseTestDir}) - st.stderr.match(function (output) { - t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message') - st.kill() - return true - }) - st.stdout.empty() - st.end() - }) - test('auth - login works', function (t) { var cmd = dat + ' login --email=hello@bob.com --password=joe' var st = spawn(t, cmd, {cwd: baseTestDir})