brutaljs
Version:
BrutalJS ========
33 lines (29 loc) • 860 B
text/coffeescript
jwt = require('jwt-simple')
moment = require('moment')
CONFIG = process.env
User = require('../../models/user')
module.exports = (req, res, next) ->
if req.body? and req.body.email? and req.body.password?
User.findOne({email: req.body.email}, (err, user) ->
if err?
res.status(400).send(err)
else
if user?
if !user.comparePassword(req.body.password)
res.status(401).send()
else
expires = moment().add(7, 'days').valueOf()
token = jwt.encode({
user: user._id
exp: expires
}, CONFIG.JWT_SECRET)
userJson = user.toJSON()
res.send({
token: token
user: userJson
})
else
res.status(401).send('No user with that email.')
)
else
res.send(401)