Spaces:
Sleeping
Sleeping
File size: 429 Bytes
b63786a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | const jwt = require('jsonwebtoken');
const optionalAuth = (req, res, next) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
next();
return;
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.userId = decoded.userId;
} catch (error) {
// Ignore invalid optional auth and continue as a public request.
}
next();
};
module.exports = optionalAuth;
|