const jwt = require('jsonwebtoken');
function Auth (req, res, next) {
const token = req.session.token || req.cookies.token;
if (!token) {
return res.status(401).send({ error: 'Unauthorized' });
}
try {
const payload = jwt.verify(token, "JWT_SECRET");
req.user = payload;
next();
} catch (error) {
res.status(401).send({ error: 'Unauthorized' });
}
}
module.exports = Auth;
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter