I made a back-end in js node and mysql to be able to login and access to secure routes with jwt. Until now i'm able to login and i get a token. I created a route protected by a middleware (like the code below) but when i try to get this route i always get a 401 Unauthorized and error 'Your session is not valid'. I follow this tutorial step by step but doesn't work for me. The complete code is in the link for details.
Thanks in advance:)
module.exports = {
isLoggedIn: (req, res, next) => {
try {
const token = req.headers.authorization.split(' ')[1];
const decoded = jwt.verify(
token,
'SECRETKEY'
);
req.userData = decoded;
next();
} catch (err) {
return res.status(401).send({
msg: 'Your session is not valid!'
});
}
}
};
I see that you figure it out
my point is (or my problem) is with this line of code
req.headers.authorization.split(' ')[1]
that's wrong what's going to happen when I just sent you a token like
tokennnnnnnnnnn
so my fix is something like that (usually it's going to be 'Bearer')
so why not something like that
req.header.authorization.replace(/bearer/i, '')
something like that I think it's going to be nice! <3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.