I am creating one angular2 app.
export const routes: Route[] = [
{path: '',redirectTo: "login",pathMatch: "full" },
{path: 'login', component: LoginComponent },
{path: 'dashboard', component: DashboardComponent,canActivate:[AuthGuard], children:
[
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'A', component: AComponent },
{ path: 'B', component: BComponent },
{ path: 'C', component: CComponent },
{ path: 'D', component: DComponent },
]
}];
When i login to my app using LoginComponent it will go to DashboardComponent which have four child components
- -A
- -B
- -C
- -D
Now i have by default set redirectTo to my dashboard component. but in place of this redirect i want to redirect to route A,B,C,D on the basis of type of login like wherether he is admin, superadmin, student or teacher.
Suppose
If Login User is "Admin" he should be redirectTo - > dashboard/A
If Login User is "Teacher" then he should be redirectTo - >dashboard/B
i have create authGuard like this
@Injectable()
export class AuthGuard implements CanActivate {
constructor(public router: Router){ }
canActivate(){
if(localStorage.getItem('userData')){
return true;
}
// this.router.navigateByUrl('/');
return false;
}
}
export class activateEmployee implements CanActivate {
constructor(){ }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
console.log(localStorage.getItem('userData'), "employee");
if(localStorage.getItem('userData') == 'superadmin'){
return true;
}
// this.router.navigateByUrl('/');
return false;
}
}
export class activateSuperAdmin implements CanActivate {
constructor(public router: Router){ }
canActivate(){
console.log(localStorage.getItem('userData'), "Superadmin");
if(localStorage.getItem('userData') == 'superadmin'){
return true;
}
return false;
}
}
export class activateAdmin implements CanActivate {
constructor(public router: Router){ }
canActivate(){
console.log(localStorage.getItem('userData'),"Admin");
if(localStorage.getItem('userData') == 'admin'){
return true;
}
return false;
}
}
PS: my main goal is to protect route that if someone knows the URL of protected route even than he is not able to go to there. or we can say i want something like multiple authGuard in a single service.
Update
Now i have created different classes for routing, but i am getting this error dont know why ? error is
Can't resolve all parameters for activateAdmin: (?).