0

I am setting a guard in creditscore childpages but it seems that it is not working. I am trying to redirect back to /creditscore page if user is not authenticated, But when i access /crediscore/verification page it is not redirecting back to /creditscore page even if guard is enabled. I have tried this code below.

const routes: Routes = [
  {
    path: '**',
    component: CreditScoreComponent,
    resolve: { config: WPConfigResolver },
    children: [
      {
        path: 'verification',
        component: CreditScoreSelectorComponent,
        canActivate: [ResultsGuard],
      },
      {
        path: 'results',
        component: CreditScoreResultsComponent,
        canActivate: [ResultsGuard],
      }
    ]
  }
];

export const routing: ModuleWithProviders = RouterModule.forChild(routes);

This is my results.guard.ts

canActivate(): boolean  {
    if (this.authService.isAuthenticatedUser()) {
      return true;
    } else {
      this.router.navigate(['/creditscore']);
      return false;
    }
  }

Is this the correct way? Thanks in advance.

raf
  • 165
  • 1
  • 14
  • What do you mean by 'redirecting to _the_ page'? Which page? Maybe mention the path you are trying to navigate to and the path you are ultimately ending up on. – MS_AU Dec 10 '19 at 23:53
  • I've updated the question. What i want is redirect back to creditscore landing page if user is not authenticated. – raf Dec 10 '19 at 23:57
  • Have you checked what value is returned by `this.authService.isAuthenticatedUser()`? `You can put console.log(this.authService.isAuthenticatedUser())` before the if statement in guard. There might be a possibility that this method returning `true` every time. – Plochie Dec 11 '19 at 04:27
  • it is returning as expected. if authenticated it returns `true` else `false`. I just don't get it why is the guard is not working. Maybe it has something to do with the `path: '**'`? – raf Dec 11 '19 at 06:10
  • can you provide stacklitz? – Plochie Dec 11 '19 at 06:15

0 Answers0