auth/auth.service.js
/**
 * Attaches the user object to the request if authenticated
 * Otherwise returns 403
 */
Attaches the user object to the request if authenticated
Otherwise returns 403
Attaches the user object to the request if authenticated Otherwise returns 403
function isAuthenticated(){
    return compose()
    .use((req,res,next)=>{
        if(req.query && req.query.hasOwnProperty('access_token')){
            req.headers.authorization = `Bearer ${req.query.access_token}`;
        }
        if(!req.headers.authorization)
            return res.status(403).send('Forbidden');
        validateJwt(req,res,next);
    })
    .use((req,res,next)=>{
        User.findById(req.user._id).exec().then(user =>{
            if(!user){return res.status(401).end();}
            req.user = user;
            next();
        }).catch(err => next(err));
    }); 
}
function isAuthenticated ( ) { return compose ( ) . use ( ( req , res , next ) => { if ( req . query && req . query . hasOwnProperty ( 'access_token' ) ) { req . headers . authorization = ` ${ req . query . access_token } ` ; } if ( ! req . headers . authorization ) return res . status ( 403 ) . send ( 'Forbidden' ) ; validateJwt ( req , res , next ) ; } ) . use ( ( req , res , next ) => { User . findById ( req . user . _id ) . exec ( ) . then ( user => { if ( ! user ) { return res . status ( 401 ) . end ( ) ; } req . user = user ; next ( ) ; } ) . catch ( err => next ( err ) ) ; } ) ; }
Attaches the user object to the request if authenticated
 Otherwise returns 403
Attaches the user object to the request if authenticated Otherwise returns 403
//Validate JWT //Attach User to request