2015-02-26 11:47:56 -04:00
( function ( ) {
2015-10-21 09:37:25 -04:00
var Promise , _ , events , form , resin , validation , visuals ;
2015-03-13 08:53:31 -04:00
2015-08-13 14:50:36 -04:00
Promise = require ( 'bluebird' ) ;
2015-08-12 08:17:46 -04:00
_ = require ( 'lodash' ) ;
2015-02-26 11:47:56 -04:00
resin = require ( 'resin-sdk' ) ;
2015-07-27 15:08:55 +03:00
form = require ( 'resin-cli-form' ) ;
2015-02-26 11:47:56 -04:00
2015-07-30 14:48:03 +03:00
visuals = require ( 'resin-cli-visuals' ) ;
2015-09-01 00:39:48 +03:00
events = require ( 'resin-cli-events' ) ;
2015-10-21 09:37:25 -04:00
validation = require ( '../utils/validation' ) ;
2015-10-21 08:18:14 -04:00
2015-02-26 11:47:56 -04:00
exports . login = {
2015-10-21 08:18:14 -04:00
signature : 'login' ,
2015-02-26 11:47:56 -04:00
description : 'login to resin.io' ,
2015-10-21 08:18:14 -04:00
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'
}
] ,
2015-10-01 10:39:36 -04:00
primary : true ,
2015-02-26 11:47:56 -04:00
action : function ( params , options , done ) {
2015-11-16 10:11:08 -04:00
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
} ) ;
2015-10-21 08:18:14 -04:00
} ) . 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' ) ;
2015-08-13 14:50:36 -04:00
} ) ;
} ) ;
2015-09-05 20:17:34 +03:00
} ) . then ( resin . auth . whoami ) . tap ( function ( username ) {
2015-09-01 00:39:48 +03:00
console . info ( "Successfully logged in as: " + username ) ;
return events . send ( 'user.login' ) ;
2015-08-13 14:50:36 -04:00
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . logout = {
signature : 'logout' ,
description : 'logout from resin.io' ,
2015-03-03 10:14:16 -04:00
help : 'Use this command to logout from your resin.io account.o\n\nExamples:\n\n $ resin logout' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-09-01 00:39:48 +03:00
return resin . auth . logout ( ) . then ( function ( ) {
return events . send ( 'user.logout' ) ;
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . signup = {
signature : 'signup' ,
description : 'signup to resin.io' ,
2015-08-13 14:50:36 -04: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 11:47:56 -04:00
action : function ( params , options , done ) {
2015-08-13 14:50:36 -04:00
return form . run ( [
{
message : 'Email:' ,
name : 'email' ,
type : 'input' ,
2015-10-21 09:37:25 -04:00
validate : validation . validateEmail
2015-08-13 14:50:36 -04:00
} , {
message : 'Username:' ,
name : 'username' ,
type : 'input'
} , {
message : 'Password:' ,
name : 'password' ,
type : 'password' ,
2015-10-21 09:37:25 -04:00
validate : validation . validatePassword
2015-02-26 11:47:56 -04:00
}
2015-09-01 00:39:48 +03:00
] ) . then ( resin . auth . register ) . then ( resin . auth . loginWithToken ) . tap ( function ( ) {
return events . send ( 'user.signup' ) ;
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . whoami = {
signature : 'whoami' ,
2015-08-13 14:50:36 -04: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 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-13 14:50:36 -04:00
return Promise . props ( {
username : resin . auth . whoami ( ) ,
2015-11-16 10:11:08 -04:00
email : resin . auth . getEmail ( ) ,
url : resin . settings . get ( 'resinUrl' )
2015-08-13 14:50:36 -04:00
} ) . then ( function ( results ) {
2015-11-16 10:11:08 -04:00
return console . log ( visuals . table . vertical ( results , [ '$account information$' , 'username' , 'email' , 'url' ] ) ) ;
2015-07-23 01:06:53 +03:00
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
} ) . call ( this ) ;