first commit
This commit is contained in:
27
lib/jwtUtils.js
Normal file
27
lib/jwtUtils.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
const generateToken = (userId) => {
|
||||
const token = jwt.sign({ userId }, "[)51k:7W71Ki+^p:;XxE4LQ£-I@B49", {
|
||||
expiresIn: "7d",
|
||||
});
|
||||
return token;
|
||||
};
|
||||
|
||||
const verifyToken = (req, res, next) => {
|
||||
const token = req.headers.authorization;
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({ message: "Unauthorized: Token missing" });
|
||||
}
|
||||
|
||||
jwt.verify(token, "[)51k:7W71Ki+^p:;XxE4LQ£-I@B49", (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).json({ message: "Unauthorized: Invalid token" });
|
||||
}
|
||||
|
||||
req.userId = decoded.userId;
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = { generateToken, verifyToken };
|
||||
Reference in New Issue
Block a user