Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created June 10, 2014 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanoats/6727e7b704da215a80d6 to your computer and use it in GitHub Desktop.
Save ivanoats/6727e7b704da215a80d6 to your computer and use it in GitHub Desktop.
// 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