canActivate not fired after navigate to "same" page
See original GitHub issueI’m submitting a …
[X] bug report
[ ] feature request
[ ] support request
Current behavior
- Default page “/” redirects to “/dashboard”
- “/dashboard” has canActivate(), it redirects to “/login”
- Login successful, then press logout button (it clears authentication)
- After logout, redirect to default page “/”
- Default page “/” redirects to “/dashboard”
- “/dashboard” has canActivate(), it is NOT fired
Expected behavior I expect canActivate() to be fired and redirect to “/login”. In the issue title, I say “same” page because the page is not the same, but perhaps the redirection is interpreted as the same page.
Minimal reproduction of the problem with instructions See my steps above. Here is my router config:
export const AppRoutes: ModuleWithProviders = RouterModule.forRoot([
// Default page
{ path: '', pathMatch: 'full', redirectTo: '/dashboard' },
// Login page is outside the protected routes
{ path: 'login', component: LoginComponent },
// Group of protected routes
{
path: '',
canActivate: [AuthGuardService],
canActivateChild: [AuthGuardService],
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'users', component: UsersListComponent },
]
},
// Default page, when route does not match a rule
// If not authenticated, user is sent to the login screen first
{ path: '**', component: PageNotFoundComponent, canActivate: [AuthGuardService] }
]);
Here is my AuthGuardService:
export class AuthGuardService implements CanActivate, CanActivateChild {
constructor(private authService: AuthService, private router: Router) {
// empty...
}
canActivate(activatedRoute: ActivatedRouteSnapshot, routerState: RouterStateSnapshot): boolean
{
if (this.authService.isAuthenticated()) {
return true;
}
this.authService.setRedirectUrl(routerState.url);
this.router.navigateByUrl('/login');
return false;
}
canActivateChild(activatedRoute: ActivatedRouteSnapshot, routerState: RouterStateSnapshot): boolean {
return this.canActivate(activatedRoute, routerState);
}
}
What is the motivation / use case for changing the behavior? It should always be fired, isn’t?
Please tell us about your environment: Windows, Chrome, Angular-CLI, NPM
- Angular version:
"@angular/animations": "^4.0.1",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/forms": "^4.0.0",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.2",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
-
Browser: Chrome, but any browser will do i think
-
Language: Typescript
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
asp.net core - Angular canactivate not fire - Stack Overflow
I've configured it properly (I hope so) but it never fire when navigate through components, it goes straight to backend and I got...
Read more >CanActivate - Angular
Interface that a class can implement to be a guard deciding if a route can be activated. If all guards return true ,...
Read more >CanActivate Route Guard in Angular - YouTube
The Angular CanActivate route guard runs before we navigate to a route allowing us to cancel the navigation. In this lecture, you will...
Read more >Angular Basics: CanActivate—Introduction to Routing Guards
CanActivate basically answers the question: “Does the user have access to this route?” We use this guard to prevent access to users who...
Read more >Router tutorial: tour of heroes - Angular
The Crisis Detail appears in a child component on the same page, beneath the list ... A wildcard route can navigate to a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This was a recently added feature:
https://angular.io/api/router/Router#onSameUrlNavigation
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.