balena-cli/lib/actions/auth.coffee

53 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-12-24 15:14:30 +00:00
_ = require('lodash')
2014-12-01 14:11:00 +00:00
url = require('url')
async = require('async')
2014-11-26 17:02:22 +00:00
resin = require('../resin')
2014-12-05 16:00:15 +00:00
ui = require('../ui')
2014-12-22 16:41:14 +00:00
log = require('../log/log')
2014-12-22 16:47:12 +00:00
errors = require('../errors/errors')
permissions = require('../permissions/permissions')
2014-12-22 19:48:11 +00:00
helpers = require('../helpers/helpers')
exports.login = (params) ->
async.waterfall [
(callback) ->
if params.credentials?
2014-12-22 19:48:11 +00:00
return helpers.parseCredentials(params.credentials, callback)
else
2014-12-05 16:00:15 +00:00
return ui.widgets.login(callback)
(credentials, callback) ->
2014-11-26 17:02:22 +00:00
resin.auth.login(credentials, callback)
2014-12-22 16:47:12 +00:00
], errors.handle
2014-11-18 15:37:29 +00:00
exports.logout = permissions.user ->
2014-11-26 17:02:22 +00:00
resin.auth.logout()
2014-11-18 15:48:05 +00:00
2014-11-18 15:37:29 +00:00
exports.signup = ->
2014-12-24 15:14:30 +00:00
async.waterfall([
(callback) ->
ui.widgets.register(callback)
(credentials, callback) ->
resin.auth.register credentials, (error, token) ->
return callback(error, credentials)
(credentials, callback) ->
credentials = _.omit(credentials, 'email')
resin.auth.login(credentials, callback)
], errors.handle)
2014-12-12 14:25:32 +00:00
exports.whoami = permissions.user ->
2014-12-12 14:25:32 +00:00
resin.auth.whoami (error, username) ->
2014-12-22 16:47:12 +00:00
errors.handle(error) if error?
2014-12-12 14:25:32 +00:00
if not username?
error = new Error('Username not found')
2014-12-22 16:47:12 +00:00
errors.handle(error)
2014-12-12 14:25:32 +00:00
2014-12-22 16:41:14 +00:00
log.out(username)