title: How to redirect and then stop in node.js
body: <p>I am trying to stop users accessing certain pages whilst logged in and not logged in with node.js.</p>
  
  <p>So far I have this idea:</p>
  
  <pre><code>exports.blockIfLoggedIn = function (req, res) {
      if (req.isAuthenticated()) { //passport
          req.flash('error', 'Sorry but you can\'t access this page');
          res.redirect('/');
      }
  };
  
  MA.f.auth.blockIfLoggedIn(req, res, next);
  res.render('users/login', {
      title: 'Login'
  });
  </code></pre>
  
  <p>This will redirect the page and but it will also error in the console with:</p>
  
  <pre><code> Error: Can't set headers after they are sent.
  </code></pre>
  
  <p>I understand that this is trying to do <code>res.render('users/login')</code> function but as I've already set the page to <code>redirect(/)</code> so it can't.</p>
  
  <p>There must be some way that if <code>req.isAuthenticated()</code> is true it will redirect and then essentially do something similar to php's <code>exit()</code>?</p>