Refactor auth actions to use promises

This commit is contained in:
Juan Cruz Viotti 2015-08-13 14:50:36 -04:00
parent 7a8a3c851b
commit 70c060b124
2 changed files with 109 additions and 220 deletions

View File

@ -1,14 +1,14 @@
(function() { (function() {
var TOKEN_URL, _, async, form, open, resin, settings, url, validEmail, visuals; var Promise, TOKEN_URL, _, form, open, resin, settings, url, validEmail, visuals;
open = require('open'); Promise = require('bluebird');
open = Promise.promisify(require('open'));
_ = require('lodash'); _ = require('lodash');
url = require('url'); url = require('url');
async = require('async');
resin = require('resin-sdk'); resin = require('resin-sdk');
settings = require('resin-settings-client'); settings = require('resin-settings-client');
@ -26,30 +26,22 @@
description: 'login to resin.io', description: 'login to resin.io',
help: "Use this command to login to your resin.io account.\n\nTo login, you need your token, which is accesible from the preferences page:\n\n " + TOKEN_URL + "\n\nExamples:\n\n $ resin login\n $ resin login \"eyJ0eXAiOiJKV1Qi...\"", help: "Use this command to login to your resin.io account.\n\nTo login, you need your token, which is accesible from the preferences page:\n\n " + TOKEN_URL + "\n\nExamples:\n\n $ resin login\n $ resin login \"eyJ0eXAiOiJKV1Qi...\"",
action: function(params, options, done) { action: function(params, options, done) {
return async.waterfall([ return Promise["try"](function() {
function(callback) {
if (params.token != null) { if (params.token != null) {
return callback(null, params.token); return params.token;
} }
console.info("To login to the Resin CLI, you need your unique token, which is accesible from\nthe preferences page at " + TOKEN_URL + "\n\nAttempting to open a browser at that location..."); console.info("To login to the Resin CLI, you need your unique token, which is accesible from\nthe preferences page at " + TOKEN_URL + "\n\nAttempting to open a browser at that location...");
return open(TOKEN_URL, function(error) { return open(TOKEN_URL)["catch"](function() {
if (error != null) { return console.error("Unable to open a web browser in the current environment.\nPlease visit " + TOKEN_URL + " manually.");
console.error("Unable to open a web browser in the current environment.\nPlease visit " + TOKEN_URL + " manually."); }).then(function() {
}
return form.ask({ return form.ask({
message: 'What\'s your token? (visible in the preferences page)', message: 'What\'s your token? (visible in the preferences page)',
type: 'input' type: 'input'
}).nodeify(callback);
}); });
}, function(token, callback) { });
return resin.auth.loginWithToken(token).nodeify(callback); }).then(resin.auth.loginWithToken).then(resin.auth.whoami).tap(function(username) {
}, function(callback) { return console.info("Successfully logged in as: " + username);
return resin.auth.whoami().nodeify(callback); }).nodeify(done);
}, function(username, callback) {
console.info("Successfully logged in as: " + username);
return callback();
}
], done);
} }
}; };
@ -66,44 +58,8 @@
exports.signup = { exports.signup = {
signature: 'signup', signature: 'signup',
description: 'signup to resin.io', description: 'signup to resin.io',
help: 'Use this command to signup for a resin.io account.\n\nIf signup is successful, you\'ll be logged in to your new user automatically.\n\nExamples:\n\n $ resin signup\n Email: me@mycompany.com\n Username: johndoe\n Password: ***********\n\n $ resin signup --email me@mycompany.com --username johndoe --password ***********\n\n $ resin whoami\n johndoe', help: 'Use this command to signup for a resin.io account.\n\nIf signup is successful, you\'ll be logged in to your new user automatically.\n\nExamples:\n\n $ resin signup\n Email: me@mycompany.com\n Username: johndoe\n Password: ***********\n\n $ resin whoami\n johndoe',
options: [
{
signature: 'email',
parameter: 'email',
description: 'user email',
alias: 'e'
}, {
signature: 'username',
parameter: 'username',
description: 'user name',
alias: 'u'
}, {
signature: 'password',
parameter: 'user password',
description: 'user password',
alias: 'p'
}
],
action: function(params, options, done) { action: function(params, options, done) {
var hasOptionCredentials;
hasOptionCredentials = !_.isEmpty(options);
if (hasOptionCredentials) {
if (options.email == null) {
return done(new Error('Missing email'));
}
if (options.username == null) {
return done(new Error('Missing username'));
}
if (options.password == null) {
return done(new Error('Missing password'));
}
}
return async.waterfall([
function(callback) {
if (hasOptionCredentials) {
return callback(null, options);
}
return form.run([ return form.run([
{ {
message: 'Email:', message: 'Email:',
@ -130,32 +86,21 @@
return true; return true;
} }
} }
]).nodeify(callback); ]).then(resin.auth.register).then(resin.auth.loginWithToken).nodeify(done);
}, function(credentials, callback) {
return resin.auth.register(credentials)["return"](credentials).nodeify(callback);
}, function(credentials, callback) {
return resin.auth.login(credentials).nodeify(callback);
}
], done);
} }
}; };
exports.whoami = { exports.whoami = {
signature: 'whoami', signature: 'whoami',
description: 'get current username', description: 'get current username and email address',
help: 'Use this command to find out the current logged in username.\n\nExamples:\n\n $ resin whoami', help: 'Use this command to find out the current logged in username and email address.\n\nExamples:\n\n $ resin whoami',
permission: 'user', permission: 'user',
action: function(params, options, done) { action: function(params, options, done) {
return resin.auth.whoami().then(function(username) { return Promise.props({
if (username == null) { username: resin.auth.whoami(),
throw new Error('Username not found'); email: resin.auth.getEmail()
} }).then(function(results) {
return resin.auth.getEmail().then(function(email) { return console.log(visuals.table.vertical(results, ['$account information$', 'username', 'email']));
return console.log(visuals.table.vertical({
username: username,
email: email
}, ['$account information$', 'username', 'email']));
});
}).nodeify(done); }).nodeify(done);
} }
}; };

View File

@ -1,7 +1,7 @@
open = require('open') Promise = require('bluebird')
open = Promise.promisify(require('open'))
_ = require('lodash') _ = require('lodash')
url = require('url') url = require('url')
async = require('async')
resin = require('resin-sdk') resin = require('resin-sdk')
settings = require('resin-settings-client') settings = require('resin-settings-client')
form = require('resin-cli-form') form = require('resin-cli-form')
@ -26,11 +26,8 @@ exports.login =
$ resin login "eyJ0eXAiOiJKV1Qi..." $ resin login "eyJ0eXAiOiJKV1Qi..."
""" """
action: (params, options, done) -> action: (params, options, done) ->
Promise.try ->
async.waterfall [ return params.token if params.token?
(callback) ->
return callback(null, params.token) if params.token?
console.info """ console.info """
To login to the Resin CLI, you need your unique token, which is accesible from To login to the Resin CLI, you need your unique token, which is accesible from
@ -39,29 +36,21 @@ exports.login =
Attempting to open a browser at that location... Attempting to open a browser at that location...
""" """
open TOKEN_URL, (error) -> open(TOKEN_URL).catch ->
if error?
console.error """ console.error """
Unable to open a web browser in the current environment. Unable to open a web browser in the current environment.
Please visit #{TOKEN_URL} manually. Please visit #{TOKEN_URL} manually.
""" """
.then ->
form.ask form.ask
message: 'What\'s your token? (visible in the preferences page)' message: 'What\'s your token? (visible in the preferences page)'
type: 'input' type: 'input'
.nodeify(callback)
(token, callback) -> .then(resin.auth.loginWithToken)
resin.auth.loginWithToken(token).nodeify(callback) .then(resin.auth.whoami)
.tap (username) ->
(callback) ->
resin.auth.whoami().nodeify(callback)
(username, callback) ->
console.info("Successfully logged in as: #{username}") console.info("Successfully logged in as: #{username}")
return callback() .nodeify(done)
], done
exports.logout = exports.logout =
signature: 'logout' signature: 'logout'
@ -92,50 +81,10 @@ exports.signup =
Username: johndoe Username: johndoe
Password: *********** Password: ***********
$ resin signup --email me@mycompany.com --username johndoe --password ***********
$ resin whoami $ resin whoami
johndoe johndoe
''' '''
options: [
{
signature: 'email'
parameter: 'email'
description: 'user email'
alias: 'e'
}
{
signature: 'username'
parameter: 'username'
description: 'user name'
alias: 'u'
}
{
signature: 'password'
parameter: 'user password'
description: 'user password'
alias: 'p'
}
]
action: (params, options, done) -> action: (params, options, done) ->
hasOptionCredentials = not _.isEmpty(options)
if hasOptionCredentials
if not options.email?
return done(new Error('Missing email'))
if not options.username?
return done(new Error('Missing username'))
if not options.password?
return done(new Error('Missing password'))
async.waterfall [
(callback) ->
return callback(null, options) if hasOptionCredentials
form.run [ form.run [
message: 'Email:' message: 'Email:'
name: 'email' name: 'email'
@ -159,21 +108,16 @@ exports.signup =
return true return true
] ]
.nodeify(callback)
(credentials, callback) -> .then(resin.auth.register)
resin.auth.register(credentials).return(credentials).nodeify(callback) .then(resin.auth.loginWithToken)
.nodeify(done)
(credentials, callback) ->
resin.auth.login(credentials).nodeify(callback)
], done
exports.whoami = exports.whoami =
signature: 'whoami' signature: 'whoami'
description: 'get current username' description: 'get current username and email address'
help: ''' help: '''
Use this command to find out the current logged in username. Use this command to find out the current logged in username and email address.
Examples: Examples:
@ -181,11 +125,11 @@ exports.whoami =
''' '''
permission: 'user' permission: 'user'
action: (params, options, done) -> action: (params, options, done) ->
resin.auth.whoami().then (username) -> Promise.props
if not username? username: resin.auth.whoami()
throw new Error('Username not found') email: resin.auth.getEmail()
resin.auth.getEmail().then (email) -> .then (results) ->
console.log visuals.table.vertical { username, email }, [ console.log visuals.table.vertical results, [
'$account information$' '$account information$'
'username' 'username'
'email' 'email'