From 4645ad06bccf13c9e88f77efba2fac4578f807e6 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 13 Jan 2016 00:25:31 -0400 Subject: [PATCH] Redirect users to signup from login if they don't have an account --- build/actions/auth.js | 43 +++++++++++++++++++++++---------------- build/utils/patterns.js | 3 +++ lib/actions/auth.coffee | 38 ++++++++++++++++++++-------------- lib/utils/patterns.coffee | 3 +++ 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/build/actions/auth.js b/build/actions/auth.js index c1c9ac49..85c64bfd 100644 --- a/build/actions/auth.js +++ b/build/actions/auth.js @@ -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); } diff --git a/build/utils/patterns.js b/build/utils/patterns.js index 719e3539..36141962 100644 --- a/build/utils/patterns.js +++ b/build/utils/patterns.js @@ -79,6 +79,9 @@ limitations under the License. }, { name: 'Authentication token', value: 'token' + }, { + name: 'I don\'t have a Resin account!', + value: 'register' } ] }); diff --git a/lib/actions/auth.coffee b/lib/actions/auth.coffee index 1c15c443..0f8bfbc8 100644 --- a/lib/actions/auth.coffee +++ b/lib/actions/auth.coffee @@ -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) diff --git a/lib/utils/patterns.coffee b/lib/utils/patterns.coffee index fcaec301..ace9c491 100644 --- a/lib/utils/patterns.coffee +++ b/lib/utils/patterns.coffee @@ -62,6 +62,9 @@ exports.askLoginType = -> , name: 'Authentication token' value: 'token' + , + name: 'I don\'t have a Resin account!' + value: 'register' ] exports.selectDeviceType = ->