Sunday, 7 May 2017

ExpressJS - Middleware Too Many Redirects

I am trying to use two middleware with my /app routes that checks for user authentication and then the status of their account. I have both middleware in place, but I am running into an endless redirect in instances where my req.session.accountStatus does not equal the conditions I have provided it. In general, I am trying to force the user to only have access to the page being redirected to. Am I using middleware in the wrong way? Is there a better approach?

function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()){
        return next();
    }
    res.redirect('/login');
}

function accountStatus(req, res, next) {
    if(req.session.accountStatus == "active" || req.session.accountStatus == "trialing"){
        return next();
    } else {
        //Endless loop. Need to fix
        res.redirect('/app/settings/billing');
    }
}

router.use(require('./site-routes'));
router.use('/app', isLoggedIn, accountStatus, require('./app-routes'));



via cphill

No comments:

Post a Comment