question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When CanActivate guard redirects to / on initial load, router does not render component

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior When a CanActivate guard navigates to the root path / on the initial load, the router does not render the root component. If the guard navigates elsewhere (e.g. /elsewhere) then it correctly renders the component for that route.

Expected behavior Navigating to root should have the same behavior as navigating to any other route. It should always render.

Minimal reproduction of the problem with instructions See https://github.com/nertzy/angular-issue-13530

What is the motivation / use case for changing the behavior? This used to work in Angular 2.2.x. We want to redirect to the root path when a user is unauthenticated in our application.

  • Angular version: We’ve seen it in 2.3.1 and 2.4.1

  • Browser: all

  • Language: TypeScript

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:35 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
kwkellycommented, Oct 9, 2018

Still seeing this in angular 6 as well. @grigorig did you find a workaround?

4reactions
Sauruzcommented, Oct 11, 2017

I got the same issue in Angular 4. My guard was looking like this:

this._authService.isAuthenticated().subscribe(authenticated => {
          return authenticated;
});

Fixed it by doing this:

 return this._authService.isAuthenticated().map(authenticated => {
        return authenticated;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

CanActivate guard redirects to / on initial load, router-outlet ...
In your routes, You need to specify the component property for the parent component(claim component) also.
Read more >
Better Redirects in Angular Route Guards | juri.dev
If the user has the necessary permissions, the route will be activated, otherwise he/she will be redirected to the “not authorized” route, ...
Read more >
Router tutorial: tour of heroes - Angular
This tutorial provides an extensive overview of the Angular router. In this tutorial, you will build upon a basic router configuration to explore...
Read more >
Implementing Route Protection in Angular using CanActivate
CanActivate · route: It contains information about the route associated with the component loaded in the router outlet. · state: It includes ...
Read more >
Use Angular Route-Guards to Secure Angular Pages
We can add a route guard by implementing the CanActivate interface available from the @angular/router package and extends the canActivate() method which holds ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found