18 lines
447 B
JavaScript
18 lines
447 B
JavaScript
const jwt = require("jsonwebtoken");
|
|
|
|
const sameUser = (req, res, next) => {
|
|
// get token
|
|
const token = req.cookies.JWT_TOKEN;
|
|
// extract username from token
|
|
const username = jwt.verify(token, process.env.SECRET_KEY);
|
|
// if different usernames
|
|
if (username != req.params.username) {
|
|
return res.status(403).json({
|
|
message: "Not authorized!",
|
|
});
|
|
}
|
|
// otherwise all good
|
|
return next();
|
|
};
|
|
|
|
module.exports = sameUser;
|