-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthMiddleware.js
More file actions
30 lines (27 loc) · 1014 Bytes
/
authMiddleware.js
File metadata and controls
30 lines (27 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const jwt = require('jsonwebtoken');
/**
* JWT Authentication Middleware
*
* Validates the JWT token from the x-auth-token header and extracts user information.
* If the token is valid, adds the user object to req.user and calls next().
* If the token is missing or invalid, returns a 401 Unauthorized response.
*
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
module.exports = function(req, res, next) {
const token = req.header('x-auth-token');
if (!token) {
return res.status(401).json({ message: 'No token provided, authorization denied' });
}
try {
// Verify and decode the JWT token
const secret = process.env.JWT_SECRET || 'dev-secret-change-me';
const decoded = jwt.verify(token, secret);
req.user = decoded.user;
next();
} catch (err) {
res.status(401).json({ message: 'Token is invalid' });
}
};