mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-06 02:56:27 +01:00
20 lines
538 B
JavaScript
20 lines
538 B
JavaScript
const jwt = require('jsonwebtoken');
|
|
|
|
module.exports = function (req, res, next) {
|
|
// Get token from header
|
|
const token = req.header('Authorization')?.replace('Bearer ', '');
|
|
|
|
// Check if no token
|
|
if (!token) {
|
|
return res.status(401).json({ message: 'No token, authorization denied' });
|
|
}
|
|
|
|
try {
|
|
// Verify token
|
|
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'your-secret-key');
|
|
req.user = decoded;
|
|
next();
|
|
} catch (err) {
|
|
res.status(401).json({ message: 'Token is not valid' });
|
|
}
|
|
};
|