From 9a89e3c3cadb501a6b99e872343a2be744128d16 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 25 Nov 2015 09:24:09 -0400 Subject: [PATCH] Perform automatic token exchange with the dashboard --- build/actions/auth.js | 49 +++++------------------------------------ lib/actions/auth.coffee | 44 +++--------------------------------- package.json | 1 + 3 files changed, 9 insertions(+), 85 deletions(-) diff --git a/build/actions/auth.js b/build/actions/auth.js index 7178c1c8..d302991c 100644 --- a/build/actions/auth.js +++ b/build/actions/auth.js @@ -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); diff --git a/lib/actions/auth.coffee b/lib/actions/auth.coffee index d92c092d..8a347a12 100644 --- a/lib/actions/auth.coffee +++ b/lib/actions/auth.coffee @@ -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}") diff --git a/package.json b/package.json index f35eb141..f818e76b 100644 --- a/package.json +++ b/package.json @@ -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",