I have this piece of code that works fine on postman when I set the header to the token generated from the access token. but when I'm using the same piece of code on the browser when I log in, I get the authorization access, but the moment I manually input the new router, I get an unauthorized error.
const verifyToken = (req, res, next) => {
const authHeader = req.headers["authorization"];
if (authHeader) {
const token = authHeader.split(" ")[1];
jwt.verify(token, process.env.jwtToken, (err, user) => {
if (err) {
res.status(500).json("Invalid Token");
} else {
req.user = user;
next();
}
});
} else {
res.status(500).json("You are not authenticated");
}
};
const verifyAndAuth = (req, res, next) => {
verifyToken(req, res, () => {
if (req.params.id === req.user.id) {
next();
} else {
res.status(500).json("You are not verified");
}
});
};
the verifyAndAuth middleware is added to every route for example:
router.get("/", admin, async (req, res) => {
try {
const noteData = await users.find();
res.status(200).json(noteData)
} catch (err) {
res.status(500).json(err)
}
});