下面是我的canActivate的auth.gurad.ts代码。我想实施基于角色的身份验证,以便在投资者登录时应将其重定向到投资者仪表板,任何帮助都将得到
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const currentUser = this.authenticationService.currentUserValue;
if (currentUser) {
debugger;
switch(currentUser.role){
case 'Business':
this.router.navigate(['/business']);
break;
case 'Invester':
this.router.navigate(['/invester']);
break;
case 'Admin':
this.router.navigate(['/admin']);
break;
}
// check if route is restricted by role
if (route.data.roles && route.data.roles.indexOf(currentUser.role) === -1) {
// role not authorised so redirect to home page
this.router.navigate(['/']);
return false;
}
// authorised so return true
return true;
}
我的routing.module.ts代码如下
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard],
data: { roles: [Role.Admin] }
},
{
path: 'invester',
component: InvesterComponent,
canActivate: [AuthGuard],
data: { roles: [Role.Invester] }
},
{
path: '',
component: HomeComponent,
canActivate: [AuthGuard]
},