mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-18 21:27:51 +00:00
Merge pull request #272 from resin-io/jviotti/feature/automatic-login-exchange
Perform automatic token exchange with the dashboard
This commit is contained in:
commit
f8bcc9d1ea
@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var Promise, _, events, form, resin, validation, visuals;
|
||||
var Promise, _, auth, events, form, resin, validation, visuals;
|
||||
|
||||
Promise = require('bluebird');
|
||||
|
||||
@ -13,57 +13,18 @@
|
||||
|
||||
events = require('resin-cli-events');
|
||||
|
||||
auth = require('resin-cli-auth');
|
||||
|
||||
validation = require('../utils/validation');
|
||||
|
||||
exports.login = {
|
||||
signature: 'login',
|
||||
description: 'login to resin.io',
|
||||
help: 'Use this command to login to your resin.io account.\n\nExamples:\n\n $ resin login',
|
||||
options: [
|
||||
{
|
||||
signature: 'email',
|
||||
parameter: 'email',
|
||||
description: 'email',
|
||||
alias: ['e', 'u']
|
||||
}, {
|
||||
signature: 'password',
|
||||
parameter: 'password',
|
||||
description: 'password',
|
||||
alias: 'p'
|
||||
}
|
||||
],
|
||||
primary: true,
|
||||
action: function(params, options, done) {
|
||||
return resin.settings.get('resinUrl').then(function(resinUrl) {
|
||||
console.log("Logging in to " + resinUrl);
|
||||
return form.run([
|
||||
{
|
||||
message: 'Email:',
|
||||
name: 'email',
|
||||
type: 'input',
|
||||
validate: validation.validateEmail
|
||||
}, {
|
||||
message: 'Password:',
|
||||
name: 'password',
|
||||
type: 'password'
|
||||
}
|
||||
], {
|
||||
override: options
|
||||
});
|
||||
}).then(resin.auth.login).then(resin.auth.twoFactor.isPassed).then(function(isTwoFactorAuthPassed) {
|
||||
if (isTwoFactorAuthPassed) {
|
||||
return;
|
||||
}
|
||||
return form.ask({
|
||||
message: 'Two factor auth challenge:',
|
||||
name: 'code',
|
||||
type: 'input'
|
||||
}).then(resin.auth.twoFactor.challenge)["catch"](function() {
|
||||
return resin.auth.logout().then(function() {
|
||||
throw new Error('Invalid two factor authentication code');
|
||||
});
|
||||
});
|
||||
}).then(resin.auth.whoami).tap(function(username) {
|
||||
console.info('Connecting to the web dashboard');
|
||||
return auth.login().then(resin.auth.whoami).tap(function(username) {
|
||||
console.info("Successfully logged in as: " + username);
|
||||
return events.send('user.login');
|
||||
}).nodeify(done);
|
||||
|
@ -4,6 +4,7 @@ resin = require('resin-sdk')
|
||||
form = require('resin-cli-form')
|
||||
visuals = require('resin-cli-visuals')
|
||||
events = require('resin-cli-events')
|
||||
auth = require('resin-cli-auth')
|
||||
validation = require('../utils/validation')
|
||||
|
||||
exports.login =
|
||||
@ -16,49 +17,10 @@ exports.login =
|
||||
|
||||
$ resin login
|
||||
'''
|
||||
options: [
|
||||
{
|
||||
signature: 'email'
|
||||
parameter: 'email'
|
||||
description: 'email'
|
||||
alias: [ 'e', 'u' ]
|
||||
}
|
||||
{
|
||||
signature: 'password'
|
||||
parameter: 'password'
|
||||
description: 'password'
|
||||
alias: 'p'
|
||||
}
|
||||
]
|
||||
primary: true
|
||||
action: (params, options, done) ->
|
||||
resin.settings.get('resinUrl')
|
||||
.then (resinUrl) ->
|
||||
console.log("Logging in to #{resinUrl}")
|
||||
|
||||
return form.run [
|
||||
message: 'Email:'
|
||||
name: 'email'
|
||||
type: 'input'
|
||||
validate: validation.validateEmail
|
||||
,
|
||||
message: 'Password:'
|
||||
name: 'password'
|
||||
type: 'password'
|
||||
],
|
||||
override: options
|
||||
.then(resin.auth.login)
|
||||
.then(resin.auth.twoFactor.isPassed)
|
||||
.then (isTwoFactorAuthPassed) ->
|
||||
return if isTwoFactorAuthPassed
|
||||
return form.ask
|
||||
message: 'Two factor auth challenge:'
|
||||
name: 'code'
|
||||
type: 'input'
|
||||
.then(resin.auth.twoFactor.challenge)
|
||||
.catch ->
|
||||
resin.auth.logout().then ->
|
||||
throw new Error('Invalid two factor authentication code')
|
||||
console.info('Connecting to the web dashboard')
|
||||
auth.login()
|
||||
.then(resin.auth.whoami)
|
||||
.tap (username) ->
|
||||
console.info("Successfully logged in as: #{username}")
|
||||
|
@ -48,6 +48,7 @@
|
||||
"nplugm": "^3.0.0",
|
||||
"president": "^2.0.1",
|
||||
"prettyjson": "^1.1.3",
|
||||
"resin-cli-auth": "^1.0.0",
|
||||
"resin-cli-errors": "^1.0.0",
|
||||
"resin-cli-events": "^1.0.2",
|
||||
"resin-cli-form": "^1.3.0",
|
||||
|
Loading…
Reference in New Issue
Block a user