Created
June 10, 2014 21:58
-
-
Save ivanoats/6727e7b704da215a80d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/jwtAuth.js | |
'use strict'; | |
var User = require('../models/User'); | |
var jwt = require('jwt-simple'); | |
module.exports = function(app) { | |
var jwtauth = {}; | |
jwtauth.auth = function(req, res, next) { | |
var token = req.body.jwt_token; | |
if (token) { | |
try { | |
var decoded = jwt.decode(token, app.get('jwtTokenSecret')); | |
User.findOne({'_id': decoded.iss}, function(err, user) { | |
if(err) { return res.send(500,err); } | |
req.user = user; | |
return next(); | |
}); | |
} catch(err) { | |
return res.send(500); | |
// log the error... | |
} | |
} else { | |
return res.send(401, {'msg': 'no access token found.'}); | |
} | |
}; | |
return jwtauth; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment