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;