-1

I'm using nativescript-urlhandler in my Nativescript Aplication. When I put a router, my application routing in first in LoginFirstComponent and in second in ResetPassIdComponent that I want.

I want to routing directly to component that I want.

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [AuthGuard],
    children: [
      {
        path: 'fp', component: FirstPageComponent
      },
      {
        path: 'profile', component: ProfileComponent
      }
    ]
  },
  {
    path: 'outsidelogin',
    component: outsideloginComponent,
    canActivate: [AuthGuardReset],
    children: [
      { path: 'login', component: LoginFirstComponent },
      { path: 'register', component: RegisterComponent },
      { path: 'resetPasswordRequest/:id', component: ResetPassIdComponent }
    ]
  },

    { path: '', redirectTo: '/home/fp', pathMatch: 'full' }
];

AuthGuard.ts

canActivate(): boolean {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        this.router.navigate(['/outsidelogin/login']);
        return false;
    }

AuthGuardReset.ts

 canActivate(): boolean {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
        return false;
    }

My AuthGuardReset.ts do not navigate to the ResetPassIdComponent, it stays in the LoginFirstComponent.

In component.ts I have this code:

ngOnInit() {
        if (this.auth.isAuthenticated()) {
            return true;
        }
        handleOpenURL((appURL: AppURL) => {
            console.log('Got the following appURL', appURL);
            this.myappurl = appURL
            let url_1 = this.myappurl.toString();
            let url_id = url_1.split("/").reverse()[0];
            this.resetpasss = url_id
            this.router.navigateByUrl('/outsidelogin/resetPasswordRequest/' + this.resetpasss);
        });
    }

Can you share with me any idea how to fix this problem please?

web site
  • 99
  • 8

1 Answers1

0

Try this :- returing true instead of false

 canActivate(): boolean {
    if (this.auth.isAuthenticated()) {
        return true;
    }
    this.router.navigate(['/outsidelogin/resetPasswordRequest/:id']);
    return true;
}

Please refer this example link for more details How to use angular 6 Route Auth Guards for all routes Root and Child Routes?

Akash Gupta
  • 34
  • 10
  • I click a link, but the application don't open, don't navigate. – web site Dec 21 '18 at 09:43
  • Please refer this example link for more details https://stackoverflow.com/questions/50485433/how-to-use-angular-6-route-auth-guards-for-all-routes-root-and-child-routes – Akash Gupta Dec 21 '18 at 09:46
  • Yes I look this example, but I think that my prob is different. Thank you – web site Dec 21 '18 at 09:53
  • can you make the example in https://stackblitz.com/ hear so i can help you out in this – Akash Gupta Dec 21 '18 at 09:55
  • I think that my problem is in handleOpenURL because it is executed twice, in first for appURL and in second for `this.router.navigateByUrl('/outsidelogin/resetPasswordRequest/' + this.resetpasss);` – web site Dec 21 '18 at 10:20
  • please put your code in https://stackblitz.com/ i will edit your code to solve your issue – Akash Gupta Dec 21 '18 at 10:55
  • Is difficult, because I use https://github.com/hypery2k/nativescript-urlhandler Nativescript in mobile and I can't modify android:pathPattern – web site Dec 21 '18 at 11:22
  • Here https://play.nativescript.org/?template=play-ng&id=E4U0vn is my project Link. I want to navigate in this component RestePassComponent where i click link. Thank you1 – web site Dec 24 '18 at 10:36
  • Can you see my demo please github.com/binaau/auth_guard After installed, please click this link stackoverflow.com/questions/53921510 and open with app. In this moment you can see that application in first navigate in login component and in second in reset pass. I want to navigate directly in reset pass only when I click link. Thank you! – web site Dec 27 '18 at 10:37