balena-cli/lib/actions/auth.coffee

42 lines
997 B
CoffeeScript
Raw Normal View History

2014-12-01 14:11:00 +00:00
url = require('url')
2014-11-18 15:37:29 +00:00
open = require('open')
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-05 14:53:59 +00:00
signupUrl = resin.settings.get('urls.signup')
absUrl = url.resolve(resin.settings.get('remoteUrl'), signupUrl)
2014-12-01 14:11:00 +00:00
open(absUrl)
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)