balena-cli/lib/actions/auth.coffee

173 lines
3.6 KiB
CoffeeScript
Raw Normal View History

open = require('open')
_ = require('lodash-contrib')
2014-12-01 14:11:00 +00:00
url = require('url')
async = require('async')
2015-01-08 12:04:37 +00:00
resin = require('resin-sdk')
visuals = require('resin-cli-visuals')
2015-03-19 19:57:43 +00:00
exports.whoami =
signature: 'whoami'
description: 'whoami'
help: '''
Use this command to get the logged in user name.
Examples:
$ resin whoami
'''
permission: 'user'
action: (params, options, done) ->
resin.auth.whoami (error, username) ->
return done(error) if error?
console.log(username)
return done()
TOKEN_URL = 'https://dashboard.resin.io/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) ->
console.info """
To login to the Resin CLI, you need your unique token, which is accesible from
the preferences page at #{TOKEN_URL}
Attempting to open a browser at such location...
"""
2015-03-11 18:37:24 +00:00
async.waterfall([
(callback) ->
open TOKEN_URL, (error) ->
if error?
console.error """
Unable to open a web browser in the current environment.
Please visit #{TOKEN_URL} manually.
"""
return callback()
(callback) ->
2015-03-11 18:37:24 +00:00
return callback(null, params.token) if params.token?
visuals.widgets.ask('What\'s your token? (visible in the preferences page)', null, callback)
2014-11-18 15:37:29 +00:00
2015-03-11 18:37:24 +00:00
(token, callback) ->
resin.auth.loginWithToken(token, done)
2014-11-18 15:48:05 +00:00
2015-03-11 18:37:24 +00:00
], 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(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
2015-02-09 15:43:10 +00:00
$ resin signup --email me@mycompany.com --username johndoe --password ***********
$ resin whoami
johndoe
'''
2015-02-09 15:43:10 +00:00
options: [
{
signature: 'email'
parameter: 'email'
description: 'user email'
alias: 'e'
}
{
signature: 'username'
parameter: 'username'
description: 'user name'
alias: 'u'
}
{
signature: 'password'
parameter: 'user password'
description: 'user password'
alias: 'p'
}
]
action: (params, options, done) ->
2015-02-09 15:43:10 +00:00
hasOptionCredentials = not _.isEmpty(options)
if hasOptionCredentials
if not options.email?
return done(new Error('Missing email'))
if not options.username?
return done(new Error('Missing username'))
if not options.password?
return done(new Error('Missing password'))
async.waterfall([
(callback) ->
2015-02-09 15:43:10 +00:00
return callback(null, options) if hasOptionCredentials
visuals.widgets.register(callback)
(credentials, callback) ->
resin.auth.register credentials, (error, token) ->
return callback(error, credentials)
(credentials, callback) ->
resin.auth.login(credentials, callback)
], done)
exports.whoami =
signature: 'whoami'
description: 'get current username'
help: '''
Use this command to find out the current logged in username.
Examples:
$ resin whoami
'''
permission: 'user'
action: (params, options, done) ->
resin.auth.whoami (error, username) ->
if not username?
return done(new Error('Username not found'))
console.log(username)