Skip to content
Permalink
d97acb7881
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
41 lines (34 sloc) 963 Bytes
import jwt from "jsonwebtoken";
export const verifyToken = (req,res,next) => {
const token = req.cookies.access_token;
if (!token) {
return res.status(404).send("Not Authenticated")
}
jwt.verify(token, process.env.JWT, (err, user) => {
if (err) {
return res.status(404).send("Token unverified");
}
req.user = user;
next()
})
}
export const verifyUser = (req, res, next) => {
verifyToken(req, res,next, () => {
if (req.user.id == req.params.id) {
next()
} else {
if (err)
return res.status(400).send("Token is not valid")
}
})
}
export const verifyAdmin = (req, res, next) => {
verifyToken(req, res, next,() => {
if (req.user.isAdmin) {
next();
} else {
if (err)
return res.status(400).send("Token is not valid");
}
});
};