Skip to content

Instantly share code, notes, and snippets.

@ryanore
Last active August 29, 2015 14:11
Show Gist options
  • Save ryanore/914362881d2d9f0878f2 to your computer and use it in GitHub Desktop.
Save ryanore/914362881d2d9f0878f2 to your computer and use it in GitHub Desktop.
MISSING_HEADER error on jwt.verify()
var config = require('../../app-config');
var User = require('../models/user-model');
var jwt = require('jsonwebtoken');
var sendStatus = function(res, stat) {
var status = stat;
var http = require('http');
res.status(status).end(http.STATUS_CODES[status]);
}
exports.verify = function(req, res) {
var token = (req.body && req.body.token) || (req.query && req.query.token) || req.headers['authorization'];
if (token) {
jwt.verify(token, config.secret, function(err, decoded) {
console.log(err); // missing header
console.log(decoded); // undefined
});
} else {
// sendStatus(res, 401);
res.end()
}
};
/**
* Create a new "session" by returning a new token
*
*/
exports.create = function(req, res) {
User.findOne({
username: req.body.username
}, function(err, usr) {
if (err || !usr) {
return sendStatus(res, 401);
}
usr.comparePassword(req.body.password, function(err, isMatch) {
if (err) throw err;
if (!isMatch) {
sendStatus(res, 401);
} else {
var token = jwt.sign(usr, config.secret, {
expiresInMinutes: 60 * 24
});
delete usr.password;
res.json({
token: token,
user: usr
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment