【发布时间】:2020-11-16 20:03:17
【问题描述】:
早上好, 我创建了一个 angualr 应用程序,我需要检查用户在路由中的角色。 我使用 canLoad 方法创建了一个服务“RoleGuardService”,该方法读取 JWS 令牌并检查用户权限:
import * as JWT from 'jwt-decode';
...
const tokenPayload: App = JWT(token);
if(tokenPayload.UserType == expectedRole){
return true;
}
return false;
到目前为止一切顺利,但这迫使我声明硬编码的权限:
{ path: 'xxx', component: yyy, canLoad: [RoleGuardService], data: { expectedRole: 'Admin' } },
是否可以创建需要直接从 Web API 授权的方法? 喜欢:
var isAllowed = false;
this.http.get('https://xxx/check_user/').subscribe(result: bool) => {
isAllowed = result;
}
///wait until the subscribe is called
return isAllowed;
【问题讨论】:
标签: angular authentication jwt angular-router-guards