Angular 10 routing not working in cordova

Viewed 281

I have a problem in the router when I want to wrap to a Cordova app for android. The problem seems to be in router module but I can't figured what is going wrong.

The problem is this Everything works well in the angular app when I run it in a browser. All the routes are working correctly and I made a deploy which also works correctly. But when I want to build the apk with the compiled code in "dist" folder, it seems like the routing is not working.

This is my principal routes imported in the app module

const routes: Routes = [
{
  path: '',
  component: AppLayoutComponent,
  canActivate: [AuthGuard],
  children: [
      { path: '', loadChildren: () => import('./pages/modules/dashboards/dashboards.module').then(m => m.DashboardsModule),canActivate: [CheckTokenGuard] },
      { path: 'orders', pathMatch: 'full',  loadChildren: () => import('./pages/modules/orders/orders.module').then(m => m.OrdersModule), canActivate: [CheckTokenGuard] }
  ]
},
{
  path: '',
  component: AuthLayoutComponent,
  children: [
    { path: 'session', loadChildren: () => import('./pages/modules/session/session.module').then(m => m.SessionModule) }
  ]
},
{
  path: '**',
  redirectTo: 'session/not-found'
}
];

@NgModule({
imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Then, the auth guard looks like this:

export class AuthAdminGuard implements CanActivate {
  constructor(private router: Router) { }

  public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (localStorage.getItem('sessiontoken')) {
      if (JSON.parse(localStorage.getItem('sessiontoken')).type === 'ADMIN') {
        return true;
      }
    }

    // not logged in so redirect to login page with the return url
    localStorage.removeItem('sessiontoken');
    this.router.navigate(['session/signin']);
    return false;
  }
}

Auth layout component looks like this:

<div>
  <router-outlet></router-outlet>
</div>

As I say, this is actually working in localhost and even in my server, but when I generate the apk It automatically redirects to the "session/not-found" path like it doesn't recognize the root "session/signin".

I build the app using ng build --aot --output-hashing all and also I change the base href with <base href="./"> and put the cordova script in the index.html

I tried everything but nothing seems to work. Pleas help

0 Answers
Related