Search code examples
pushstateaureliaaurelia-auth

Aurelia Push State App Reload on Login and Logout


When configuring Push State with Aurelia and Visual Studio, I am getting an odd behavior where after I select login my entire app reloads instead of the router just pushing to the homepage. This also happens when I logout, I get to the login screen and it refreshes the entire app. I am using Aurelia Auth. Any assistance would be much appreciated.


Solution

  • I think I had the exact same issue some time ago and this was one of the reasons I switched back to pushState = false (but my infos may be helpful for you).

    Anyways, the following issue describes what I was facing: https://github.com/paulvanbladel/aurelia-auth/issues/55

    The problem is, internally the plugin sets href:

    Login - https://github.com/paulvanbladel/aurelia-auth/blob/master/src/authentication.js#L95-L99

    if (this.config.loginRedirect && !redirect) {
      window.location.href = this.getLoginRedirect();
    } else if (redirect && isString(redirect)) {
      window.location.href = window.encodeURI(redirect);
    }
    

    Logout - https://github.com/paulvanbladel/aurelia-auth/blob/master/src/authentication.js#L139-L143

    if (this.config.logoutRedirect && !redirect) {
      window.location.href = this.config.logoutRedirect;
    } else if (isString(redirect)) {
      window.location.href = redirect;
    }
    

    What you need to do is avoid both conditions, i.e. set loginRedirect and logoutRedirect to the empty string (''). Then, do the navigation on your own via Aurelias router as I did in my example from the GH issue:

    return this.auth.login(userInfo)
      .then(response => {
        console.log('You signed in successfully.');
        this.router.navigate('/contents');
      })
    

    Of course, do the same router navigation on your logout method.