I am creating one angular2-meteor app.
export const routes: Route[] = [{
path: \'\',
redirectTo: \"login\",
pathMatch: \"full\"
},
{
path:
In routing module, u need to create local variable "route",check condition and assign requires routing path the variable and assign variable to redirectTo in routing
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
var route: string = "user";
const role = JSON.parse(localStorage.getItem("currentUser")).roleId;
if (role == "1") {
//superadmin role
route = "project"
} else {
//normal user role
route = "user"
}
const adminRoutes: Routes = [ {
path: '',
component: AdminComponent,
canActivate: [AuthGuard],
children: [
{ path: '', redirectTo: route },
{
path: 'project',
loadChildren: './project-dashboard/project-dashboard.module#ProjectModule'
}, {
path: 'user',
loadChildren: './user/user.module#UserModule',
}
Here is the better solution: Guard the admin feature according to Angular guide.
Using CanActivate hook
Steps:
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { Router } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
canActivate() {
//Your redirect logic/condition. I use this.
if (this.user && this.user.profile.role == 'Guest') {
this.router.navigate(['dashboard']);
}
console.log('AuthGuard#canActivate called');
return true;
}
//Constructor
constructor(private router: Router) { }
}
import { AuthGuard } from './auth-guard.service';
import { AdminComponent } from './admin/admin.component';
@NgModule({
declarations: [
AppComponent,
AdminComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot([
{
path: 'admin',
component: AdminComponent,
canActivate:[AuthGuard]
},
{
path: '',
redirectTo: '/dashboard',
pathMatch: 'full'
}
])
],
providers: [AuthGuard],
bootstrap: [AppComponent]
})
export class AppModule { }
**Reference ** https://angular.io/docs/ts/latest/guide/router.html#!#can-activate-guard