diff --git a/blockchain_integration/pi_network/middleware/authMiddleware.js b/blockchain_integration/pi_network/middleware/authMiddleware.js new file mode 100644 index 000000000..f4feb15f8 --- /dev/null +++ b/blockchain_integration/pi_network/middleware/authMiddleware.js @@ -0,0 +1,23 @@ +// middleware/authMiddleware.js + +const jwt = require('jsonwebtoken'); + +const SECRET_KEY = 'YOUR_SECRET_KEY'; // Replace with your actual secret key + +const authMiddleware = (req, res, next) => { + const token = req.headers['authorization']?.split(' ')[1]; // Bearer + + if (!token) { + return res.status(401).json({ message: 'No token provided, authorization denied.' }); + } + + jwt.verify(token, SECRET_KEY, (err, decoded) => { + if (err) { + return res.status(403).json({ message: 'Token is not valid.' }); + } + req.user = decoded; // Attach user info to request object + next(); // Proceed to the next middleware or route handler + }); +}; + +module.exports = authMiddleware;