[英]Component is loading before canActivate returns the result in angular 4 when integrated with keycloak
I have integrated keycloak with application, I have written AuthGuard to protect specific routes.canActivate works fine but the component for the route also loading and another API calls also getting triggered in background. 我已经将keycloak与应用程序集成在一起,我编写了AuthGuard来保护特定的路由。canActivate可以正常工作,但是该路由的组件也正在加载,并且另一个API调用也在后台触发。 How to load component until canActivate returns the result.
在canActivate返回结果之前如何加载组件。
AuthGuard:
-----------
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
if (!this.keycloakService.getIsClientSecretKeyFetch()) {
this.errorHandler._enlivenErrorhandler.handleError({"status":"401","message" :"Unable to get client details. Please contact your administrator ","url":window.location.href});
this.router.navigate(['404'], { queryParams: { tenantId: this.cookieService.get('tenantId') } });
}
this.isAccessAllowed(route,state);
return super.canActivate(route, state);
}
isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
return new Promise((resolve, reject) => {
if (!this.authenticated) {
this.keycloakAngular.login();
return;
}
const path = route.data.path;
this.keycloakService.isAuthorized(path).then(
(res) => {
let access = res;
if (access) {
resolve(true);
} else {
resolve(false);
}
});
resolve(true);
});
}
routes.ts
-----------
RouterModule.forChild([
{
path: 'data-source-mongo-list',
canActivate: [AppAuthGuard],
data: {
path:'data-source-mongo-list'
},
component: DataSourceListComponent
}])
From what I see it looks like you implemented all that OpenId stuff by yourself. 从我看来,您似乎自己实现了所有OpenId。 From my personal experience I can't recommend this.
根据我的个人经验,我不推荐这样做。 Did you try to use angular-oauth2-oidc?
您是否尝试过使用angular-oauth2-oidc? I think it is well described here: https://www.softwarearchitekt.at/post/2016/07/03/authentication-in-angular-2-with-oauth2-oidc-and-guards-for-the-newest-new-router-english-version.aspx
我认为它在这里得到了很好的描述: https : //www.softwarearchitekt.at/post/2016/07/03/authentication-in-angular-2-with-oauth2-oidc-and-guards-for-the-newest-新的路由器-英语- version.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.