mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-27 06:20:14 +00:00
Refactor auth actions to use promises
This commit is contained in:
parent
7a8a3c851b
commit
70c060b124
@ -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 params.token;
|
||||||
return callback(null, 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 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);
|
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)["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'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}).then(resin.auth.loginWithToken).then(resin.auth.whoami).tap(function(username) {
|
||||||
|
return console.info("Successfully logged in as: " + username);
|
||||||
|
}).nodeify(done);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -66,96 +58,49 @@
|
|||||||
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;
|
return form.run([
|
||||||
hasOptionCredentials = !_.isEmpty(options);
|
{
|
||||||
if (hasOptionCredentials) {
|
message: 'Email:',
|
||||||
if (options.email == null) {
|
name: 'email',
|
||||||
return done(new Error('Missing email'));
|
type: 'input',
|
||||||
}
|
validate: function(input) {
|
||||||
if (options.username == null) {
|
if (!validEmail(input)) {
|
||||||
return done(new Error('Missing username'));
|
return 'Email is not valid';
|
||||||
}
|
|
||||||
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:',
|
|
||||||
name: 'email',
|
|
||||||
type: 'input',
|
|
||||||
validate: function(input) {
|
|
||||||
if (!validEmail(input)) {
|
|
||||||
return 'Email is not valid';
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}, {
|
|
||||||
message: 'Username:',
|
|
||||||
name: 'username',
|
|
||||||
type: 'input'
|
|
||||||
}, {
|
|
||||||
message: 'Password:',
|
|
||||||
name: 'password',
|
|
||||||
type: 'password',
|
|
||||||
validate: function(input) {
|
|
||||||
if (input.length < 8) {
|
|
||||||
return 'Password should be 8 characters long';
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]).nodeify(callback);
|
return true;
|
||||||
}, function(credentials, callback) {
|
}
|
||||||
return resin.auth.register(credentials)["return"](credentials).nodeify(callback);
|
}, {
|
||||||
}, function(credentials, callback) {
|
message: 'Username:',
|
||||||
return resin.auth.login(credentials).nodeify(callback);
|
name: 'username',
|
||||||
|
type: 'input'
|
||||||
|
}, {
|
||||||
|
message: 'Password:',
|
||||||
|
name: 'password',
|
||||||
|
type: 'password',
|
||||||
|
validate: function(input) {
|
||||||
|
if (input.length < 8) {
|
||||||
|
return 'Password should be 8 characters long';
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
], done);
|
]).then(resin.auth.register).then(resin.auth.loginWithToken).nodeify(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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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,42 +26,31 @@ exports.login =
|
|||||||
$ resin login "eyJ0eXAiOiJKV1Qi..."
|
$ resin login "eyJ0eXAiOiJKV1Qi..."
|
||||||
"""
|
"""
|
||||||
action: (params, options, done) ->
|
action: (params, options, done) ->
|
||||||
|
Promise.try ->
|
||||||
|
return params.token if params.token?
|
||||||
|
|
||||||
async.waterfall [
|
console.info """
|
||||||
|
To login to the Resin CLI, you need your unique token, which is accesible from
|
||||||
|
the preferences page at #{TOKEN_URL}
|
||||||
|
|
||||||
(callback) ->
|
Attempting to open a browser at that location...
|
||||||
return callback(null, params.token) if params.token?
|
"""
|
||||||
|
|
||||||
console.info """
|
open(TOKEN_URL).catch ->
|
||||||
To login to the Resin CLI, you need your unique token, which is accesible from
|
console.error """
|
||||||
the preferences page at #{TOKEN_URL}
|
Unable to open a web browser in the current environment.
|
||||||
|
Please visit #{TOKEN_URL} manually.
|
||||||
Attempting to open a browser at that location...
|
|
||||||
"""
|
"""
|
||||||
|
.then ->
|
||||||
|
form.ask
|
||||||
|
message: 'What\'s your token? (visible in the preferences page)'
|
||||||
|
type: 'input'
|
||||||
|
|
||||||
open TOKEN_URL, (error) ->
|
.then(resin.auth.loginWithToken)
|
||||||
if error?
|
.then(resin.auth.whoami)
|
||||||
console.error """
|
.tap (username) ->
|
||||||
Unable to open a web browser in the current environment.
|
console.info("Successfully logged in as: #{username}")
|
||||||
Please visit #{TOKEN_URL} manually.
|
.nodeify(done)
|
||||||
"""
|
|
||||||
|
|
||||||
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) ->
|
|
||||||
console.info("Successfully logged in as: #{username}")
|
|
||||||
return callback()
|
|
||||||
|
|
||||||
], done
|
|
||||||
|
|
||||||
exports.logout =
|
exports.logout =
|
||||||
signature: 'logout'
|
signature: 'logout'
|
||||||
@ -92,88 +81,43 @@ 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) ->
|
||||||
|
form.run [
|
||||||
|
message: 'Email:'
|
||||||
|
name: 'email'
|
||||||
|
type: 'input'
|
||||||
|
validate: (input) ->
|
||||||
|
if not validEmail(input)
|
||||||
|
return 'Email is not valid'
|
||||||
|
|
||||||
hasOptionCredentials = not _.isEmpty(options)
|
return true
|
||||||
|
,
|
||||||
|
message: 'Username:'
|
||||||
|
name: 'username'
|
||||||
|
type: 'input'
|
||||||
|
,
|
||||||
|
message: 'Password:'
|
||||||
|
name: 'password'
|
||||||
|
type: 'password',
|
||||||
|
validate: (input) ->
|
||||||
|
if input.length < 8
|
||||||
|
return 'Password should be 8 characters long'
|
||||||
|
|
||||||
if hasOptionCredentials
|
return true
|
||||||
|
]
|
||||||
|
|
||||||
if not options.email?
|
.then(resin.auth.register)
|
||||||
return done(new Error('Missing email'))
|
.then(resin.auth.loginWithToken)
|
||||||
|
.nodeify(done)
|
||||||
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'
|
|
||||||
type: 'input'
|
|
||||||
validate: (input) ->
|
|
||||||
if not validEmail(input)
|
|
||||||
return 'Email is not valid'
|
|
||||||
|
|
||||||
return true
|
|
||||||
,
|
|
||||||
message: 'Username:'
|
|
||||||
name: 'username'
|
|
||||||
type: 'input'
|
|
||||||
,
|
|
||||||
message: 'Password:'
|
|
||||||
name: 'password'
|
|
||||||
type: 'password',
|
|
||||||
validate: (input) ->
|
|
||||||
if input.length < 8
|
|
||||||
return 'Password should be 8 characters long'
|
|
||||||
|
|
||||||
return true
|
|
||||||
]
|
|
||||||
.nodeify(callback)
|
|
||||||
|
|
||||||
(credentials, callback) ->
|
|
||||||
resin.auth.register(credentials).return(credentials).nodeify(callback)
|
|
||||||
|
|
||||||
(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,13 +125,13 @@ 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'
|
||||||
]
|
]
|
||||||
.nodeify(done)
|
.nodeify(done)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user