I am facing an issue, my app is getting crashed with JWT Token Expired Error. I have changed the secret key also but still same issue I am facing.
Here is the code :
Mddleware:
const auth = async (req, res, next) => {
try {
const token = req.headers.authorization.split(" ")[1];
const isCustomAuth = token?.length < 500;
if (!token)
{return res.status(401).json({ message: "No token provided" });}
let decodedData;
if(token && isCustomAuth){
decodedData= verifyToken(token);
req.userId = decodedData?.id;
}
else{
decodedData = jwt.decode(token);
console.log(decodedData + "decoded token");
req.userId = decodedData?.sub;
}
next();
} catch (error) {
console.log(error);
res.status(403).json({ message: "Token is not valid" });
throw Error();
}
};
Token verification
import jwt from "jsonwebtoken";
const secret = process.env.SECRET;
export const generateToken = (data) => {
try {
return jwt.sign({ email: data.email, id: data._id }, secret , { expiresIn: "24h" });
} catch(e) {
throw new Error(e)
}
}
export const verifyToken = (token) => {
try {
return jwt.verify(token, secret);
} catch(e) {
throw new Error(e)
}
}
The app is getting crashed saying jwt expired error. Please help.