0

I have the conditional navigation based on customers role in the system. Something like this:

this.GetQuickStartStatus()
                    .subscribe(data => {
                            if(data.isActive){
                                this.storageService.Store('isQuick', "true");
                            }
                            else{
                                this.storageService.Store('isQuick', "false");
                            }

                            if(this.storageService.Retrieve("Role") === "user"){
                                console.log("package");
                                this.router.navigate(['/packages']);
                            }

                            if(this.storageService.Retrieve("Role") === "subscriber" && this.storageService.Retrieve("isQuick") === "false"){
                                console.log("quickstart");
                                this.router.navigate(['/quickstart']);
                            }
                            console.log("nothing");
                            this.router.navigate(['']);
                    }, (error) => {
                        console.log(error);
                    });

I log with the new user which should redirect to packages page since users role is "user. I get his in the console:

auth.service.ts:156 package
auth.service.ts:164 nothing

The meaning condition gets hit, but doesn't do anything aka router doesn't navigate to the requested page. I checked all conditions are met and all values are stored in localstorage. Why would router.navigate ignore redirection?

Routing module for packages:

@NgModule({
  imports: [
    RouterModule.forChild([
        { path: 'packages', component: PackageComponent, canActivate: [AuthGuardService] },
        { path: 'receipt', component: PackageReceiptComponent, canActivate: [AuthGuardService, RoleSubscriberGuardService] },
        { path: 'receipt/:token', component: PackageReceiptComponent, canActivate: [AuthGuardService, RoleSubscriberGuardService]}
    ])
  ],
  exports: [RouterModule]
})
export class PackageRoutingModule { }
6
  • Share your routing module? Commented Mar 20, 2018 at 9:02
  • 5
    Problem isn't located here: this.router.navigate(['']); ? This line is executed every time. Try to commend it or wrap with else Commented Mar 20, 2018 at 9:05
  • I appended for packages. I have them segregated Commented Mar 20, 2018 at 9:05
  • Have you tried navigating to "/" instead of just ""? Commented Mar 20, 2018 at 9:47
  • 1
    @MaciejTreder can you please put is an answer, that was it, not sure how it worked in first try probably because of cache. clean your cache people. Commented Mar 20, 2018 at 10:06

1 Answer 1

1

You have this.router.navigate(['']); at the end of your method - which is executed every time.

Try to wrap this line in the else block.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.