auth

PHOTO EMBED

Fri Jun 07 2024 08:09:41 GMT+0000 (Coordinated Universal Time)

Saved by @sid_balar

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;
content_copyCOPY