2016-01-11 19:58:35 +00:00
/ *
Copyright 2016 Resin . io
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2015-02-26 15:47:56 +00:00
( function ( ) {
exports . login = {
2015-10-21 12:18:14 +00:00
signature : 'login' ,
2015-02-26 15:47:56 +00:00
description : 'login to resin.io' ,
2016-01-12 13:07:15 +00:00
help : 'Use this command to login to your resin.io account.\n\nThis command will prompt you to login using the following login types:\n\n- Web authorization: open your web browser and prompt you to authorize the CLI\nfrom the dashboard.\n\n- Credentials: using email/password and 2FA.\n\n- Token: using the authentication token from the preferences page.\n\nExamples:\n\n $ resin login\n $ resin login --web\n $ resin login --token "..."\n $ resin login --credentials\n $ resin login --credentials --email johndoe@gmail.com --password secret' ,
2015-12-03 14:22:22 +00:00
options : [
{
signature : 'token' ,
description : 'auth token' ,
parameter : 'token' ,
alias : 't'
2016-01-12 13:07:15 +00:00
} , {
signature : 'web' ,
description : 'web-based login' ,
boolean : true ,
alias : 'w'
2015-12-12 04:09:31 +00:00
} , {
signature : 'credentials' ,
description : 'credential-based login' ,
boolean : true ,
alias : 'c'
} , {
signature : 'email' ,
parameter : 'email' ,
description : 'email' ,
alias : [ 'e' , 'u' ]
} , {
signature : 'password' ,
parameter : 'password' ,
description : 'password' ,
alias : 'p'
2015-12-03 14:22:22 +00:00
}
] ,
2015-10-01 14:39:36 +00:00
primary : true ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var Promise , _ , auth , capitano , form , login , messages , patterns , resin ;
2016-01-12 13:07:15 +00:00
_ = require ( 'lodash' ) ;
2015-12-07 14:32:24 +00:00
Promise = require ( 'bluebird' ) ;
2016-01-13 04:25:31 +00:00
capitano = Promise . promisifyAll ( require ( 'capitano' ) ) ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
auth = require ( 'resin-cli-auth' ) ;
2016-01-12 13:07:15 +00:00
form = require ( 'resin-cli-form' ) ;
patterns = require ( '../utils/patterns' ) ;
2016-01-12 14:44:11 +00:00
messages = require ( '../utils/messages' ) ;
2016-01-12 13:07:15 +00:00
login = function ( options ) {
2015-12-03 14:22:22 +00:00
if ( options . token != null ) {
2016-01-12 13:07:15 +00:00
return Promise [ "try" ] ( function ( ) {
if ( _ . isString ( options . token ) ) {
return options . token ;
2015-12-12 04:09:31 +00:00
}
return form . ask ( {
2016-01-12 13:07:15 +00:00
message : 'Token (from the preferences page)' ,
name : 'token' ,
2015-12-12 04:09:31 +00:00
type : 'input'
} ) ;
2016-01-12 13:07:15 +00:00
} ) . then ( resin . auth . loginWithToken ) ;
} else if ( options . credentials ) {
return patterns . authenticate ( options ) ;
} else if ( options . web ) {
console . info ( 'Connecting to the web dashboard' ) ;
return auth . login ( ) ;
2015-12-03 14:22:22 +00:00
}
2016-01-12 13:07:15 +00:00
return patterns . askLoginType ( ) . then ( function ( loginType ) {
2016-01-13 04:25:31 +00:00
if ( loginType === 'register' ) {
return capitano . runAsync ( 'signup' ) ;
}
2016-01-12 13:07:15 +00:00
options [ loginType ] = true ;
return login ( options ) ;
} ) ;
} ;
return resin . settings . get ( 'resinUrl' ) . then ( function ( resinUrl ) {
2016-01-12 14:44:11 +00:00
console . log ( messages . resinAsciiArt ) ;
2016-01-12 14:23:46 +00:00
console . log ( "\nLogging in to " + resinUrl ) ;
2016-01-12 13:07:15 +00:00
return login ( options ) ;
2015-12-03 14:22:22 +00:00
} ) . then ( resin . auth . whoami ) . tap ( function ( username ) {
2015-08-31 21:39:48 +00:00
console . info ( "Successfully logged in as: " + username ) ;
2016-01-12 14:44:11 +00:00
return console . info ( "\nNow what?\n\n" + messages . gettingStarted + "\n\nFind out about more super powers by running:\n\n $ resin help\n\n" + messages . reachingOut ) ;
2015-08-13 18:50:36 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . logout = {
signature : 'logout' ,
description : 'logout from resin.io' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to logout from your resin.io account.o\n\nExamples:\n\n $ resin logout' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var resin ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
2016-02-12 18:34:16 +00:00
return resin . auth . logout ( ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . signup = {
signature : 'signup' ,
description : 'signup to resin.io' ,
2015-08-13 18:50:36 +00:00
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' ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2016-02-12 18:34:16 +00:00
var form , resin , validation ;
2015-12-07 14:32:24 +00:00
resin = require ( 'resin-sdk' ) ;
form = require ( 'resin-cli-form' ) ;
validation = require ( '../utils/validation' ) ;
2016-01-13 04:25:31 +00:00
return resin . settings . get ( 'resinUrl' ) . then ( function ( resinUrl ) {
console . log ( "\nRegistering to " + resinUrl ) ;
return form . run ( [
{
message : 'Email:' ,
name : 'email' ,
type : 'input' ,
validate : validation . validateEmail
} , {
message : 'Username:' ,
name : 'username' ,
type : 'input'
} , {
message : 'Password:' ,
name : 'password' ,
type : 'password' ,
validate : validation . validatePassword
}
] ) ;
2016-02-12 18:34:16 +00:00
} ) . then ( resin . auth . register ) . then ( resin . auth . loginWithToken ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . whoami = {
signature : 'whoami' ,
2015-08-13 18:50:36 +00:00
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' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-12-07 14:32:24 +00:00
var Promise , resin , visuals ;
Promise = require ( 'bluebird' ) ;
resin = require ( 'resin-sdk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
2015-08-13 18:50:36 +00:00
return Promise . props ( {
username : resin . auth . whoami ( ) ,
2015-11-16 14:11:08 +00:00
email : resin . auth . getEmail ( ) ,
url : resin . settings . get ( 'resinUrl' )
2015-08-13 18:50:36 +00:00
} ) . then ( function ( results ) {
2015-11-16 14:11:08 +00:00
return console . log ( visuals . table . vertical ( results , [ '$account information$' , 'username' , 'email' , 'url' ] ) ) ;
2015-07-22 22:06:53 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
} ) . call ( this ) ;