Javascript – How to redirect user after successful login

htmljavascriptparse-platform

Update:
After implementing the below suggestion by Rob Sedgwick, it has become apparent that the redirect only works when the user manually "F5" refreshers the browser (Chrome). What I need to achieve is that this happens automatically in the code so the redirect happens without the user having to hot refresh. Thanks for help with this last part.

At the moment ManageTodosView from what I understand is the first action after the user has been logged in. It prints a list of to do items set up by the user. A working example can be found here http://parseplatform.github.io/Todo/ and the code is https://github.com/ParsePlatform/Todo

I'm using to code to really get user logins to work, I'm not to worries about what the output of the rest of the code is because the long term plan will be to remove it, for the time being its helpful to keep in place to show that the app functioning correctly.

I'm using this code as a base to build a web app. At the moment, once the user is logged in, they are displayed data on the same page.

I want to be able to change this so that after they login, the user is redirected to a different page and the information is then displayed to them there.

The reason for this is that the index page is just a landing/login page and I want to redirect them to a more structured HTML page with menus, etc.

Within the JS code, do I just put in a redirect, something like:

self.location="top.htm";

to this area of the code?

// The main view for the app
  var AppView = Parse.View.extend({
    // Instead of generating a new element, bind to the existing skeleton of
    // the App already present in the HTML.
    el: $("#todoapp"),

    initialize: function() {
      this.render();
    },

    render: function() {
      if (Parse.User.current()) {
        new ManageTodosView();
      } else {
        new LogInView();
      }
    }
  });

I have added the JS code to this JSFiddle

Best Answer

Update:

To address the issue of the page needing a manual fresh before the redirect works, insert

window.location.href="/someurl";

into the following code section within the todoe.js file and comment out the new ManageTodosView(); code.

      Parse.User.logIn(username, password, {
        success: function(user) {
window.location.href="user_home.html";          
//new ManageTodosView();
          self.undelegateEvents();
          delete self;
        },