-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
blockchain_integration/pi_network/middleware/authMiddleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |