0

I am testing this example about login registration in angular stackblitz The problem is that when you are logged in and you refresh the page on browser authguard redirects to login page. I have to fix this and redirect to current page. Hope anybody can help with this. Thanks.

A.A
  • 81
  • 5
  • 16
  • Found a similar question answered https://stackoverflow.com/questions/49034663/angular-5-canactivate-redirecting-to-login-on-browser-refresh – Shameer Mohammed Apr 11 '19 at 09:05

1 Answers1

1
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const currentUser = this.authenticationService.currentUserValue;
    if (currentUser) {
        // authorised so return true
        return true;
    }
    else {
      // not logged in so redirect to login page with the return url
      this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } 
      });
      return false;
    }
 }

It's working as of now, i just made the router navigate code to go in else part .Check the stackblitz

Vaibhav
  • 1,481
  • 13
  • 17