ekangularbase
Version:
Authentication service for usermanagement(oidc client)
40 lines (36 loc) • 1.04 kB
text/typescript
// src/app/auth/role-guard.service.ts
import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot
} from '@angular/router';
import { AuthService } from './auth.service';
()
export class RoleGuardService implements CanActivate {
constructor(public auth: AuthService, public router: Router) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
// this will be passed from the route config
// on the data property
const expectedRole:string[] = route.data.expectedRole;
var rolematch: boolean = false;
var rolearray = this.auth.getRoles();
for (let x in expectedRole)
{
var r: string = expectedRole[x].toString();
if (rolearray.find(s=>s===r))
{
rolematch = true;
break;
}
}
if (
!this.auth.isLoggedIn() ||
!rolematch
) {
this.router.navigate(['dashboard']);
return false;
}
return true;
}
}