interceptors/auth.js
/**
     * This method authenticate whether the user is logged in or not
     * @param req Request object
     * @param res Response object
     * @param next Handler for next action
     * 
     */
This method authenticate whether the user is logged in or not
@param req Request object
@param res Response object
@param next Handler for next action
This method authenticate whether the user is logged in or not @param req Request object @param res Response object @param next Handler for next action
function authenticate(roles) {
    return function (req, res, next) {
        var tokenWithBearer = req.headers.authorization;
        if (tokenWithBearer) {
            var token = tokenWithBearer.split(' ')[1];
            jwt.verify(token, config.secret, function (err, decoded) {
                if (err) {
                    return res.status(401).send({ auth: false, message: 'Invalid authorization token.' });
                } else {
                    if(roles.indexOf(decoded.role) >= 0) {
                        req.auth = decoded;
                        next();
                    } else {
                        return res.status(401).send({ auth: false, message: 'Authorization failed.' });
                    }
                }
            });
        } else {
            return res.status(403).send({ auth: false, message: 'Authorization token not found.' });
        }
    }
}
function authenticate ( roles ) { return function ( req , res , next ) { var tokenWithBearer = req . headers . authorization ; if ( tokenWithBearer ) { var token = tokenWithBearer . split ( ' ' ) [ 1 ] ; jwt . verify ( token , config . secret , function ( err , decoded ) { if ( err ) { return res . status ( 401 ) . send ( { auth : false , message : 'Invalid authorization token.' } ) ; } else { if ( roles . indexOf ( decoded . role ) >= 0 ) { req . auth = decoded ; next ( ) ; } else { return res . status ( 401 ) . send ( { auth : false , message : 'Authorization failed.' } ) ; } } } ) ; } else { return res . status ( 403 ) . send ( { auth : false , message : 'Authorization token not found.' } ) ; } } }
This method authenticate whether the user is logged in or not
 @param req Request object
 @param res Response object
 @param next Handler for next action
This method authenticate whether the user is logged in or not @param req Request object @param res Response object @param next Handler for next action
// check authorization header // verifies secret and checks exp // if everything is good, save to request for use in other routes