I have an angular 4 app, and have implemented a guard to handle authentication. We have different modules which, use a factory to create what we call a ThemeService. Each module is self contained application with their own styles, and security. Each module tells the ThemeService what role the user must have to access and the guard works well.
Here's the guard:
export class AuthenticatedGuard implements CanActivate
{
constructor (private memSrv:MembershipService,
private themeSrv:ThemeService,private router:Router )
{
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return this.memSrv.GetCurrentUser()
.map(u => {
if (!u) {
this.router.navigate([this.themeSrv.LoginUrl]);
return false;
}
var isAuth = u.Capabilities.some(s => s === this.themeSrv.AuthCapability);
if (isAuth) {
return true;
}
else {
//TODO: This is an unauthorized but authenticated user it should go to an unauthorized page
this.router.navigate(["/Unauthorized", { url: state.url }]);
return false;
}
}
);
}
}
I now have a module which has some routes which need to be secured using a different role. My question is how I can pass to the AuthenticationGuard the role that must be checked for each route. I'd hate to have to create a different guard for each role we want to check.
Bellow is a snippet of my route configuration
{
path: "",
component: Component.MainComponent,
children:
[
{
path: 'attachmentsTest',
component: Component.AttachmentsTestComponent,
},
{
path:"home",
component: Component.HomeComponent,
canActivate: [AuthenticatedGuard],
resolve: {
user: CurrentUserResolver
}
},