18 lines
473 B
JavaScript
18 lines
473 B
JavaScript
// 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();
|
|
});
|
|
}
|
|
|