init
This commit is contained in:
17
backend/middleware/auth.js
Normal file
17
backend/middleware/auth.js
Normal file
@@ -0,0 +1,17 @@
|
||||
// trackaccess-backend/middleware/auth.js
|
||||
import jwt from 'jsonwebtoken';
|
||||
import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
export function authenticateToken(req, res, next) {
|
||||
const authHeader = req.headers['authorization'];
|
||||
const token = authHeader && authHeader.split(' ')[1];
|
||||
if (!token) return res.sendStatus(401);
|
||||
|
||||
jwt.verify(token, process.env.JWT_SECRET, (err, payload) => {
|
||||
if (err) return res.sendStatus(403);
|
||||
req.user = payload;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user