changed the folder structure. separated the routes and added the authmiddleware for authentication

This commit is contained in:
2024-09-18 22:07:23 +08:00
parent 023505fa0b
commit 0c9e1a4216
7 changed files with 145 additions and 118 deletions
+21
View File
@@ -0,0 +1,21 @@
const jwt = require("jsonwebtoken");
const verifyToken = (req, res, next) => {
const token = req.header("Authorization");
if (!token) {
return res.status(403).send("A token is required for authentication");
}
try {
const jwtSecretKey = process.env.JWT_SECRET_KEY || "default_secret_key";
const decoded = jwt.verify(token.replace("Bearer ", ""), jwtSecretKey);
req.user = decoded;
} catch (err) {
return res.status(401).send("Invalid Token");
}
return next();
};
module.exports = verifyToken;