var jwt = require('jsonwebtoken'); function auth(req,res,next){ const getToken = req.headers.authorization; if(!getToken){ res.status(401).json({ status: 'error', message: 'not found' }) } const tokenCheck = getToken.split(' '); if(tokenCheck.length != 2 || tokenCheck[0] !== 'Bearer'){ res.status(401).json({ status: 'error', message: 'Invalid authorization' }) } const token = tokenCheck[1]; jwt.verify(token, 'shhhhh', function(err, decoded) { if(err){ res.status(401).json({ status: 'error', message: 'faild authorization' }) } req.user = decoded; next(); }); } 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