I'm trying to write user authorization middleware. I use this function to validate and verify authorization later on.
I have done yarn add express-jwt.
auth.js code:
const SECRET = 'secret-message';
const jwt = require('jsonwebtoken');
const expressJwt = require('express-jwt');
const compose = require('composable-middleware');
function sign(user) {
return jwt.sign({
_id: user._id,
}, SECRET, {
expiresIn: 60 * 60
});
}
function sendUnauthorized(req, res) {
console.log(req.headers.authorization);
console.log(req.user);
res.status(401).json ({ message: 'Unathorized' });
};
const validateJwt = expressJwt({
secret: SECRET,
fail: sendUnauthorized,
getToken(req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.access_token) {
return req.query.access_token;
}
return null;
}
});
And the error is:
const validateJwt = expressJwt({
^
TypeError: expressJwt is not a function
I'm stuck on this error and can't go any further. I really need some advice.