I am using Angular 4 CLI project and I have angular service, angular component, interceptor, guard and other regular items.
My Angular service component has following item:
export class AdminAuthService {
adminAuthContext: AdminAuthContext;
constructor(private httpClient: HttpClient, private urlService: UrlService) {
... other code ...
this.loadSecurityContext();
}
loadSecurityContext() {
this.httpClient.get<AdminAuthContext>(`${this.urlService.url_authContext}`)
.subscribe(context => {
this.adminAuthContext = context;
}, error => Utils.logErrorResponse(error));
}
}
My Component has following code:
export class ArticleManagerComponent{
constructor(private route: ActivatedRoute, private articleService: ArticleService, private adminAuthService: AdminAuthService) {
}
isAdmin() {
let canActivate = this.adminAuthService.adminAuthContext && this.adminAuthService.adminAuthContext.claims && (this.adminAuthService.adminAuthContext.claims.find(claim => claim.type === 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' && claim.value === 'Admin'));
return canActivate;
}
}
Now, I have Component.Html
<div class="rightpane" *ngIf="isAdmin()">
<admin-article-edit [selectedArticleId]="articleEdit_articleId"></admin-article-edit>
Now, I call AdminAuthServie through my Login component and make sure that I have aminAuthContext object set.
Now, I call Article component and It call isAdmin method where I am getting AdminAuthContext undefined.
Can anyone tell why? and How to fix this?