initial commit, should work
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
const authenticate = (req, res, next) => {
|
||||
const token = req.headers.authorization?.split(" ")[1];
|
||||
if (!token) {
|
||||
return res.status(401).json({ message: "No token provided" });
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
req.user = decoded; // Attach user data to request
|
||||
next(); // Continue to the next middleware or route handler
|
||||
} catch (error) {
|
||||
return res.status(401).json({ message: "Invalid or expired token" });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = authenticate;
|
||||
Reference in New Issue
Block a user