Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirectTo is not working for children routes. #69

Closed
rajguru827 opened this issue Jul 25, 2018 · 8 comments
Closed

redirectTo is not working for children routes. #69

rajguru827 opened this issue Jul 25, 2018 · 8 comments

Comments

@rajguru827
Copy link

rajguru827 commented Jul 25, 2018

I using the shared module with lazy loading of modules. While I am importing my shared module in all modules and *ngxPermissionsOnly is working is fine.

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgxPermissionsModule } from 'ngx-permissions';

@NgModule({
  imports: [
    CommonModule,
    NgxPermissionsModule.forRoot()
  ],
  declarations: [],
  exports: [
    // Modules
    CommonModule,
    NgxPermissionsModule
  ]
})
export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [
       
      ]
    };
  }
}
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './shared/guards/auth.guard';

const routes: Routes = [
  {
    path: '', loadChildren: './user/user.module#UserModule', canActivate: [AuthGuard]
  },
  {
    path: 'auth', loadChildren: './auth/auth.module#AuthModule'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UserComponent } from './user.component';
import { NgxPermissionsGuard } from 'ngx-permissions';
import { Get103EntryResloveService } from './entry-one-zero-three/get-103-entry-reslove.service';

const routes: Routes = [
  {
    path: '',
    component: UserComponent,
    canActivateChild: [NgxPermissionsGuard],
    children: [
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'dashboard',
      },
      {
        path: 'dashboard',
        loadChildren: './dashboard/dashboard.module#DashboardModule',
        data: {
          permissions: {
            only: ['101', '102'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'employee',
        loadChildren: './employee/employee.module#EmployeeModule',
        data: {
          permissions: {
            only: ['101', '102'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'entry-103',
        loadChildren: './entry-one-zero-three/entry-one-zero-three.module#EntryOneZeroThreeModule',
        resolve: {
          get103Entry: Get103EntryResloveService
        },
        data: {
          permissions: {
            only: ['101', '102', '107'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'entry-105',
        loadChildren: './entry-one-zero-five/entry-one-zero-five.module#EntryOneZeroFiveModule',
        data: {
          permissions: {
            only: ['101', '102', '107'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'address-verification',
        loadChildren: './address-verification/address-verification.module#AddressVerificationModule',
        data: {
          permissions: {
            only: ['101', '102', '103', '104', '105', '106'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'reports',
        loadChildren: './reports/reports.module#ReportsModule',
        data: {
          permissions: {
            only: ['101', '102', '103'],
            redirectTo: '/auth/login'
          }
        }
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UserRoutingModule {}
@AlexKhymenko
Copy link
Owner

@rajguru827 Thats probably you are using lazy routes. ITs better to use https://github.com/AlexKhymenko/ngx-permissions/wiki/Usage-with-routes#can-load-guard

@rajguru827
Copy link
Author

I tried using lazy routes still it's not working..facing the same problem.

@AlexKhymenko
Copy link
Owner

@rajguru827 Can You show what you wrote ?

@rajguru827
Copy link
Author


import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UserComponent } from './user.component';
import { NgxPermissionsGuard } from 'ngx-permissions';
import { Get103EntryResloveService } from './entry-one-zero-three/get-103-entry-reslove.service';

const routes: Routes = [
  {
    path: '',
    component: UserComponent,
    children: [
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'dashboard',
      },
      {
        path: 'dashboard',
        loadChildren: './dashboard/dashboard.module#DashboardModule',
        canLoad: [NgxPermissionsGuard],
        data: {
          permissions: {
            only: ['101', '102'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'employee',
        loadChildren: './employee/employee.module#EmployeeModule',
        canLoad: [NgxPermissionsGuard],
        data: {
          permissions: {
            only: ['101', '102'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'entry-103',
        loadChildren: './entry-one-zero-three/entry-one-zero-three.module#EntryOneZeroThreeModule',
        canLoad: [NgxPermissionsGuard],
        resolve: {
          get103Entry: Get103EntryResloveService
        },
        data: {
          permissions: {
            only: ['101', '102', '107'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'entry-105',
        loadChildren: './entry-one-zero-five/entry-one-zero-five.module#EntryOneZeroFiveModule',
        canLoad: [NgxPermissionsGuard],
        data: {
          permissions: {
            only: ['101', '102', '107'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'address-verification',
        loadChildren: './address-verification/address-verification.module#AddressVerificationModule',
        canLoad: [NgxPermissionsGuard],
        data: {
          permissions: {
            only: ['101', '102', '103', '104', '105', '106'],
            redirectTo: '/auth/login'
          }
        }
      },
      {
        path: 'reports',
        loadChildren: './reports/reports.module#ReportsModule',
        canLoad: [NgxPermissionsGuard],
        data: {
          permissions: {
            only: ['101', '102', '103'],
            redirectTo: '/auth/login'
          }
        }
      }
    ]
  }
];

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

@AlexKhymenko
Copy link
Owner

@rajguru827 I will try to create reproduction. It will take some time.

@iharishsuthar
Copy link

iharishsuthar commented Jul 30, 2018

@rajguru827 i faced the same problem:

While defining a role and its permissions i was just calling addRole method, which caused the same problem, later on i have called loadPermissions method just before the addRole method having same permissions, whcih resolved the issue.

this.permissionsService.loadPermissions(["VIEW", "CREATE", "EDIT"]); this.rolesService.addRole("ADMIN", ["VIEW", "CREATE", "EDIT"]);

Hope using this will resolve you problem as well.

@AlexKhymenko
Copy link
Owner

@rajguru827 Do you still need help?

@faradoxuz
Copy link

same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants