Skip to content

Commit

Permalink
add username and register/login prompts (#34)
Browse files Browse the repository at this point in the history
* add username and register/login prompts

* email address -> email
  • Loading branch information
joehand authored Dec 16, 2016
1 parent 616620c commit aaccceb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 77 deletions.
45 changes: 32 additions & 13 deletions lib/commands/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var prompt = require('prompt')
var ui = require('../../ui')
var TownshipClient = require('../../township')

Expand All @@ -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)
})
}
}
51 changes: 38 additions & 13 deletions lib/commands/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var prompt = require('prompt')
var ui = require('../../ui')
var TownshipClient = require('../../township')

Expand All @@ -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)
})
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
52 changes: 1 addition & 51 deletions tests/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]'
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 protected] --password=joe'
var cmd = dat + ' register [email protected] --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')
Expand All @@ -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 protected]'
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 protected] --password=joe'
var st = spawn(t, cmd, {cwd: baseTestDir})
Expand Down

0 comments on commit aaccceb

Please sign in to comment.