init commit

This commit is contained in:
2025-07-07 00:52:41 +05:30
commit 2dd24927bc
81 changed files with 10274 additions and 0 deletions

View File

@ -0,0 +1,22 @@
const jwt = require("jsonwebtoken");
const User = require("../models/User");
// Middleware to protect routes
const protect = async (req, res, next) => {
try {
let token = req.headers.authorization;
if (token && token.startsWith("Bearer")) {
token = token.split(" ")[1]; // Extract token
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = await User.findById(decoded.id).select("-password");
next();
} else {
res.status(401).json({ message: "Not authorized, no token" });
}
} catch (error) {
res.status(401).json({ message: "Token failed", error: error.message });
}
};
module.exports = { protect };