2014-12-24 15:14:30 +00:00
|
|
|
_ = require('lodash')
|
2014-12-01 14:11:00 +00:00
|
|
|
url = require('url')
|
2014-11-18 16:15:40 +00:00
|
|
|
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')
|
2014-12-12 21:20:29 +00:00
|
|
|
permissions = require('../permissions/permissions')
|
2014-12-22 19:48:11 +00:00
|
|
|
helpers = require('../helpers/helpers')
|
2014-11-18 12:41:13 +00:00
|
|
|
|
2014-12-12 21:20:29 +00:00
|
|
|
exports.login = (params) ->
|
2014-11-18 16:15:40 +00:00
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
2014-12-12 21:20:29 +00:00
|
|
|
if params.credentials?
|
2014-12-22 19:48:11 +00:00
|
|
|
return helpers.parseCredentials(params.credentials, callback)
|
2014-11-18 16:15:40 +00:00
|
|
|
else
|
2014-12-05 16:00:15 +00:00
|
|
|
return ui.widgets.login(callback)
|
2014-11-18 16:15:40 +00:00
|
|
|
|
|
|
|
(credentials, callback) ->
|
2014-11-26 17:02:22 +00:00
|
|
|
resin.auth.login(credentials, callback)
|
2014-11-18 16:15:40 +00:00
|
|
|
|
2014-12-22 16:47:12 +00:00
|
|
|
], errors.handle
|
2014-11-18 15:37:29 +00:00
|
|
|
|
2014-12-12 21:20: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
|
|
|
|
2014-12-12 21:20:29 +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)
|