balena-cli/lib/actions/auth.coffee

138 lines
3.0 KiB
CoffeeScript
Raw Normal View History

2015-08-13 18:50:36 +00:00
Promise = require('bluebird')
open = Promise.promisify(require('open'))
2015-08-12 12:17:46 +00:00
_ = require('lodash')
2014-12-01 14:11:00 +00:00
url = require('url')
2015-01-08 12:04:37 +00:00
resin = require('resin-sdk')
settings = require('resin-settings-client')
2015-07-27 12:08:55 +00:00
form = require('resin-cli-form')
visuals = require('resin-cli-visuals')
validEmail = require('valid-email')
TOKEN_URL = url.resolve(settings.get('dashboardUrl'), '/preferences')
exports.login =
2015-03-11 18:37:24 +00:00
signature: 'login [token]'
description: 'login to resin.io'
help: """
Use this command to login to your resin.io account.
2015-03-11 18:37:24 +00:00
To login, you need your token, which is accesible from the preferences page:
#{TOKEN_URL}
Examples:
$ resin login
2015-03-11 18:37:24 +00:00
$ resin login "eyJ0eXAiOiJKV1Qi..."
"""
action: (params, options, done) ->
2015-08-13 18:50:36 +00:00
Promise.try ->
return params.token if params.token?
2015-08-13 18:50:36 +00:00
console.info """
To login to the Resin CLI, you need your unique token, which is accesible from
the preferences page at #{TOKEN_URL}
2015-08-13 18:50:36 +00:00
Attempting to open a browser at that location...
"""
2015-08-13 18:50:36 +00:00
open(TOKEN_URL).catch ->
console.error """
Unable to open a web browser in the current environment.
Please visit #{TOKEN_URL} manually.
"""
2015-08-13 18:50:36 +00:00
.then ->
form.ask
message: 'What\'s your token? (visible in the preferences page)'
type: 'input'
2015-08-13 18:50:36 +00:00
.then(resin.auth.loginWithToken)
.then(resin.auth.whoami)
.tap (username) ->
console.info("Successfully logged in as: #{username}")
.nodeify(done)
2014-12-24 15:14:30 +00:00
exports.logout =
signature: 'logout'
description: 'logout from resin.io'
help: '''
Use this command to logout from your resin.io account.o
2014-12-24 15:14:30 +00:00
Examples:
$ resin logout
'''
permission: 'user'
action: (params, options, done) ->
resin.auth.logout().nodeify(done)
2014-12-24 15:14:30 +00:00
exports.signup =
signature: 'signup'
description: 'signup to resin.io'
help: '''
Use this command to signup for a resin.io account.
2014-12-24 15:14:30 +00:00
If signup is successful, you'll be logged in to your new user automatically.
2014-12-12 14:25:32 +00:00
Examples:
$ resin signup
Email: me@mycompany.com
Username: johndoe
Password: ***********
2014-12-12 14:25:32 +00:00
$ resin whoami
johndoe
'''
action: (params, options, done) ->
2015-08-13 18:50:36 +00:00
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
]
.then(resin.auth.register)
.then(resin.auth.loginWithToken)
.nodeify(done)
exports.whoami =
signature: 'whoami'
2015-08-13 18:50:36 +00:00
description: 'get current username and email address'
help: '''
2015-08-13 18:50:36 +00:00
Use this command to find out the current logged in username and email address.
Examples:
$ resin whoami
'''
permission: 'user'
action: (params, options, done) ->
2015-08-13 18:50:36 +00:00
Promise.props
username: resin.auth.whoami()
email: resin.auth.getEmail()
.then (results) ->
console.log visuals.table.vertical results, [
'$account information$'
'username'
'email'
]
.nodeify(done)