title: Node.js Page Redirect on AJAX login? With a function call after redirect?
body: <p>The '/stories' page is user-specific and requires valid login credentials.</p>
  
  <p>Validation is accounted for with node, server-side, and properly logs before the redirect, but does not redirect to the stories page..</p>
  
  <p>This seems to be because "<a href="https://stackoverflow.com/questions/11570301/res-redirect-from-post">You can't make a redirection after an AJAX. You need to do it yourself in Javascript</a>", but the code in the first answer to this question seems somewhat incomplete..for the succcess call..</p>
  
  <p>Also, how can a subsequent function (refreshStories) be called on success after the redirect?</p>
  
  <p>Here's the AJAX:</p>
  
  <pre><code>if (loginUsername.length != 0) {
    console.log('there is a login: ' + loginUsername);
      // make an ajax call
      $.ajax({
      dataType: 'json',
      data: AjaxLoginData,
      type: 'post',
        url:"http://localhost:4200/api/v1/users",
        success: (data, textStatus, jqXHR) -&gt;
            if typeof data.redirect == 'string'
                window.location = data.redirect
        success: refreshStories,
        error: foundError
      });
  </code></pre>
  
  <p>Node.js + Express4</p>
  
  <pre><code>router.route('/users')
  
      // log in a user (accessed at POST http://localhost:4200/api/v1/users)
      .post(function(req, res) {
  
        var username = req.body.loginUsername;
        var password = req.body.loginPassword;
  
        authenticateUser(username, password, function(err, user){
        console.log('authenticate user..');
          if (user) {
            console.log('yes user');
            // subsequent requests will know the user is logged in
            req.session.username = user.username;
            console.log('set session username to: ' + req.session.username);
  
            // res.redirect('/stories');
                  res.send({redirect: '/stories'});
            console.log('user logged in .. redirect to stories');
  
          } else {
            console.log('user authentication badCredentials error..');
            res.render('index', {badCredentials: true});
          }
        });
      });
  </code></pre>