Redirect users to signup from login if they don't have an account

This commit is contained in:
Juan Cruz Viotti 2016-01-13 00:25:31 -04:00
parent ef7e39450c
commit 4645ad06bc
4 changed files with 54 additions and 33 deletions

View File

@ -50,9 +50,10 @@ limitations under the License.
],
primary: true,
action: function(params, options, done) {
var Promise, _, auth, events, form, login, messages, patterns, resin;
var Promise, _, auth, capitano, events, form, login, messages, patterns, resin;
_ = require('lodash');
Promise = require('bluebird');
capitano = Promise.promisifyAll(require('capitano'));
resin = require('resin-sdk');
events = require('resin-cli-events');
auth = require('resin-cli-auth');
@ -78,6 +79,9 @@ limitations under the License.
return auth.login();
}
return patterns.askLoginType().then(function(loginType) {
if (loginType === 'register') {
return capitano.runAsync('signup');
}
options[loginType] = true;
return login(options);
});
@ -119,23 +123,26 @@ limitations under the License.
form = require('resin-cli-form');
events = require('resin-cli-events');
validation = require('../utils/validation');
return form.run([
{
message: 'Email:',
name: 'email',
type: 'input',
validate: validation.validateEmail
}, {
message: 'Username:',
name: 'username',
type: 'input'
}, {
message: 'Password:',
name: 'password',
type: 'password',
validate: validation.validatePassword
}
]).then(resin.auth.register).then(resin.auth.loginWithToken).tap(function() {
return resin.settings.get('resinUrl').then(function(resinUrl) {
console.log("\nRegistering to " + resinUrl);
return form.run([
{
message: 'Email:',
name: 'email',
type: 'input',
validate: validation.validateEmail
}, {
message: 'Username:',
name: 'username',
type: 'input'
}, {
message: 'Password:',
name: 'password',
type: 'password',
validate: validation.validatePassword
}
]);
}).then(resin.auth.register).then(resin.auth.loginWithToken).tap(function() {
return events.send('user.signup');
}).nodeify(done);
}

View File

@ -79,6 +79,9 @@ limitations under the License.
}, {
name: 'Authentication token',
value: 'token'
}, {
name: 'I don\'t have a Resin account!',
value: 'register'
}
]
});

View File

@ -73,6 +73,7 @@ exports.login =
action: (params, options, done) ->
_ = require('lodash')
Promise = require('bluebird')
capitano = Promise.promisifyAll(require('capitano'))
resin = require('resin-sdk')
events = require('resin-cli-events')
auth = require('resin-cli-auth')
@ -96,6 +97,10 @@ exports.login =
return auth.login()
return patterns.askLoginType().then (loginType) ->
if loginType is 'register'
return capitano.runAsync('signup')
options[loginType] = true
return login(options)
@ -165,21 +170,24 @@ exports.signup =
events = require('resin-cli-events')
validation = require('../utils/validation')
form.run [
message: 'Email:'
name: 'email'
type: 'input'
validate: validation.validateEmail
,
message: 'Username:'
name: 'username'
type: 'input'
,
message: 'Password:'
name: 'password'
type: 'password',
validate: validation.validatePassword
]
resin.settings.get('resinUrl').then (resinUrl) ->
console.log("\nRegistering to #{resinUrl}")
form.run [
message: 'Email:'
name: 'email'
type: 'input'
validate: validation.validateEmail
,
message: 'Username:'
name: 'username'
type: 'input'
,
message: 'Password:'
name: 'password'
type: 'password',
validate: validation.validatePassword
]
.then(resin.auth.register)
.then(resin.auth.loginWithToken)

View File

@ -62,6 +62,9 @@ exports.askLoginType = ->
,
name: 'Authentication token'
value: 'token'
,
name: 'I don\'t have a Resin account!'
value: 'register'
]
exports.selectDeviceType = ->