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.

canActivate not fired after navigate to "same" page

See original GitHub issue

I’m submitting a …

[X] bug report
[  ] feature request
[  ] support request

Current behavior

  1. Default page “/” redirects to “/dashboard”
  2. “/dashboard” has canActivate(), it redirects to “/login”
  3. Login successful, then press logout button (it clears authentication)
  4. After logout, redirect to default page “/”
  5. Default page “/” redirects to “/dashboard”
  6. “/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:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonadencommented, Dec 19, 2017
0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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