Skip to content

Commit

Permalink
Create authMiddleware.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Oct 31, 2024
1 parent 9a27bef commit ef0254d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions blockchain_integration/pi_network/middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -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 <token>

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;

0 comments on commit ef0254d

Please sign in to comment.