Humberd
Humberd

Reputation: 2953

How to reload current route guards?

Let's say I have a route that has a guard AdminGuard, which lets me in only when I'm an admin.

But when I'm on that page and I click Logout the state is propagated in all the app via Observables, but I still am in a route, which I am not supposed to be in, since I've been logged out.

How to "reactivate" curent route guards?

Upvotes: 9

Views: 3365

Answers (1)

Y. Tarion
Y. Tarion

Reputation: 423

You can navigate to the same page when you click on logout.

(Some code like this should work, but it's probably not the best way.)

`

let currentUrl:string = this.router.url;
this.router.navigateByUrl('/somewhere-else').then(() => {
    this.router.navigate([currentUrl]);
});

`

A better way :

It better to use onSameUrlNavigation:'reload' on built-in Angular Router configuration. And on your route add runGuardsAndResolvers: 'always' Then you can simply navigate to the same url.

For more information about, I encourage you to read this

Upvotes: 1

Related Questions