Merge pull request #146 from resin-io/jviotti/refactor/auth

Refactor auth actions to use promises
This commit is contained in:
Juan Cruz Viotti 2015-08-17 08:41:19 -04:00
commit 25311f2a18
2 changed files with 109 additions and 220 deletions

View File

@ -1,14 +1,14 @@
(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');
url = require('url');
async = require('async');
resin = require('resin-sdk');
settings = require('resin-settings-client');
@ -26,30 +26,22 @@
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...\"",
action: function(params, options, done) {
return async.waterfall([
function(callback) {
return Promise["try"](function() {
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...");
return open(TOKEN_URL, function(error) {
if (error != null) {
console.error("Unable to open a web browser in the current environment.\nPlease visit " + TOKEN_URL + " manually.");
}
return open(TOKEN_URL)["catch"](function() {
return console.error("Unable to open a web browser in the current environment.\nPlease visit " + TOKEN_URL + " manually.");
}).then(function() {
return form.ask({
message: 'What\'s your token? (visible in the preferences page)',
type: 'input'
}).nodeify(callback);
});
}, function(token, callback) {
return resin.auth.loginWithToken(token).nodeify(callback);
}, function(callback) {
return resin.auth.whoami().nodeify(callback);
}, function(username, callback) {
console.info("Successfully logged in as: " + username);
return callback();
}
], done);
});
}).then(resin.auth.loginWithToken).then(resin.auth.whoami).tap(function(username) {
return console.info("Successfully logged in as: " + username);
}).nodeify(done);
}
};
@ -66,44 +58,8 @@
exports.signup = {
signature: 'signup',
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',
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'
}
],
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',
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([
{
message: 'Email:',
@ -130,32 +86,21 @@
return true;
}
}
]).nodeify(callback);
}, function(credentials, callback) {
return resin.auth.register(credentials)["return"](credentials).nodeify(callback);
}, function(credentials, callback) {
return resin.auth.login(credentials).nodeify(callback);
}
], done);
]).then(resin.auth.register).then(resin.auth.loginWithToken).nodeify(done);
}
};
exports.whoami = {
signature: 'whoami',
description: 'get current username',
help: 'Use this command to find out the current logged in username.\n\nExamples:\n\n $ resin whoami',
description: 'get current username and email address',
help: 'Use this command to find out the current logged in username and email address.\n\nExamples:\n\n $ resin whoami',
permission: 'user',
action: function(params, options, done) {
return resin.auth.whoami().then(function(username) {
if (username == null) {
throw new Error('Username not found');
}
return resin.auth.getEmail().then(function(email) {
return console.log(visuals.table.vertical({
username: username,
email: email
}, ['$account information$', 'username', 'email']));
});
return Promise.props({
username: resin.auth.whoami(),
email: resin.auth.getEmail()
}).then(function(results) {
return console.log(visuals.table.vertical(results, ['$account information$', 'username', 'email']));
}).nodeify(done);
}
};

View File

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