40 lines
967 B
CoffeeScript
Raw Normal View History

2014-11-14 09:51:59 -04:00
async = require('async')
2014-11-17 14:40:32 -04:00
_ = require('lodash')
2014-11-14 09:51:59 -04:00
server = require('../../server/server')
2014-11-14 09:51:59 -04:00
token = require('../../token/token')
2014-11-14 09:51:59 -04:00
exports.authenticate = (credentials, callback) ->
server.post '/login_', credentials, (error, response) ->
return callback(error, response?.body)
exports.login = (credentials, callback) ->
2014-11-14 09:51:59 -04:00
async.waterfall([
(callback) ->
exports.authenticate(credentials, callback)
(authToken, callback) ->
token.saveToken(authToken, callback)
], callback)
# Handy aliases
exports.isLoggedIn = token.hasToken
exports.getToken = token.getToken
# TODO: Maybe we should post to /logout or something
# like that to invalidate the token on the server?
exports.logout = token.clearToken
2014-11-17 14:40:32 -04:00
exports.parseCredentials = (credentials) ->
result = credentials.split(':')
if result.length isnt 2
2014-11-17 14:54:20 -04:00
throw new Error('Invalid credentials. The expected input is username:password.')
2014-11-17 14:40:32 -04:00
return {
username: _.first(result)
password: _.last(result)
}