You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

22 lines
581 B

import jwt from "jsonwebtoken";
export function isTokenValid(req, res, next) {
const logger = req.body.logger;
try {
const token = req.headers.authorization.split(' ')[1];
const decoded = jwt.verify(token, process.env.JWT_SECRET);
if (!decoded) {
logger.write("Invalid token", 401);
return res.status(401).json({error: 'Invalid token'});
} else {
next()
}
} catch (error) {
logger.write("Invalid token", 401);
return res.status(401).json({error: 'Invalid Token'});
}
}