1

The '/stories' page is user-specific and requires valid login credentials.

Validation is accounted for with node, server-side, and properly logs before the redirect, but does not redirect to the stories page..

This seems to be because "You can't make a redirection after an AJAX. You need to do it yourself in Javascript", but the code in the first answer to this question seems somewhat incomplete..for the succcess call..

Also, how can a subsequent function (refreshStories) be called on success after the redirect?

Here's the AJAX:

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) ->
          if typeof data.redirect == 'string'
              window.location = data.redirect
      success: refreshStories,
      error: foundError
    });

Node.js + Express4

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});
        }
      });
    });
Community
  • 1
  • 1
StackThis
  • 883
  • 3
  • 15
  • 45

1 Answers1

3

Try this

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) ->
          if (typeof data.redirect == 'string')
              window.location.replace(window.location.protocol + "//" + window.location.host + data.redirect);
      error: foundError
    });

window.location.replace(...) will best simulate an HTTP redirect How to redirect to another webpage in JavaScript/jQuery?

For your 'refreshStories' stuff, you should just refresh Stories when you go to '/stories'

Community
  • 1
  • 1
Oliboy50
  • 2,661
  • 3
  • 27
  • 36
  • This line `success: (data, textStatus, jqXHR) ->` logs a syntax error.. and I thought it looked unlike javascript, but I pulled it from the linked post.. – StackThis Jun 23 '14 at 13:20
  • 1
    I was assuming you use CoffeeScript. Isn't that the case ? If you're not using CoffeeScript, the correct syntax would be `success: function(data, textStatus, jqXHR){...}` – Oliboy50 Jun 23 '14 at 13:40
  • Ah, no.. javascript/jquery/handlebars/express/node.js – StackThis Jun 23 '14 at 14:00