diff --git a/connect/communityGuard/connectAdminLoginGuard.guard.ts b/connect/communityGuard/connectAdminLoginGuard.guard.ts index e718cd8e..6a1765de 100644 --- a/connect/communityGuard/connectAdminLoginGuard.guard.ts +++ b/connect/communityGuard/connectAdminLoginGuard.guard.ts @@ -22,7 +22,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanActivateChild { check(community: string, path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => { + const authorized = this.userManagementService.getUserInfo().pipe(take(1), map(user => { if (user) { if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) { return of(true); diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index 19eea1f8..aac13b37 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -30,7 +30,7 @@ export class ConnectSubscriberGuard implements CanActivate, CanActivateChild { } else { community = ConnectHelper.getCommunityFromDomain(properties.domain); } - const authorized = this.userManagementService.getUserInfo(false).pipe(take(1), map(user => { + const authorized = this.userManagementService.getUserInfo().pipe(take(1), map(user => { if (user) { if (Session.isSubscribedTo('community', community, user)) { return of(true); diff --git a/login/adminLoginGuard.guard.ts b/login/adminLoginGuard.guard.ts index 5c5fb990..0548fb3b 100644 --- a/login/adminLoginGuard.guard.ts +++ b/login/adminLoginGuard.guard.ts @@ -22,7 +22,7 @@ export class AdminLoginGuard implements CanActivate, CanActivateChild { check(data: Data, path: string): Observable { let errorCode = LoginErrorCodes.NOT_LOGIN; - return this.userManagementService.getUserInfo(false).pipe(map(user => { + return this.userManagementService.getUserInfo().pipe(map(user => { if (user) { errorCode = LoginErrorCodes.NOT_ADMIN; } diff --git a/login/claimsCuratorGuard.guard.ts b/login/claimsCuratorGuard.guard.ts index 724f0f0a..2bfce277 100644 --- a/login/claimsCuratorGuard.guard.ts +++ b/login/claimsCuratorGuard.guard.ts @@ -15,7 +15,7 @@ export class ClaimsCuratorGuard implements CanActivate { check(path: string): Observable | boolean { let errorCode = LoginErrorCodes.NOT_LOGIN; - return this.userManagementService.getUserInfo(false).pipe(map(user => { + return this.userManagementService.getUserInfo().pipe(map(user => { if (user) { errorCode = LoginErrorCodes.NOT_ADMIN; } diff --git a/login/loginGuard.guard.ts b/login/loginGuard.guard.ts index 99faad2c..59e72592 100644 --- a/login/loginGuard.guard.ts +++ b/login/loginGuard.guard.ts @@ -22,7 +22,7 @@ export class LoginGuard implements CanActivate, CanLoad, CanActivateChild { } check(path: string): Observable | boolean { - return this.userManagementService.getUserInfo(false).pipe(map(user => { + return this.userManagementService.getUserInfo().pipe(map(user => { return user !== null; }),tap(isLoggedIn => { if(!isLoggedIn) { diff --git a/login/user.component.ts b/login/user.component.ts index d364684f..1f6ee1d8 100644 --- a/login/user.component.ts +++ b/login/user.component.ts @@ -43,8 +43,8 @@ export class UserComponent { this.loginUrl = this.properties.loginUrl; if (typeof document !== 'undefined') { this.server = false; - this.subscriptions.push(this.userManagementsService.getUserInfo(false).subscribe(user => { - this.user = user; + this.userManagementsService.updateUserInfo(() => { + this.user = this.userManagementsService.user; this.loggedIn = !!this.user; this.errorMessage = ""; this.loading = true; @@ -58,7 +58,7 @@ export class UserComponent { this.loading = false; } })); - })); + }); } } diff --git a/role-verification/role-verification.component.ts b/role-verification/role-verification.component.ts index 609ecd29..c45184f8 100644 --- a/role-verification/role-verification.component.ts +++ b/role-verification/role-verification.component.ts @@ -103,7 +103,7 @@ export class RoleVerificationComponent implements OnInit, OnDestroy, AfterViewIn ngAfterViewInit() { this.subs.push(this.route.queryParams.subscribe(params => { if (params && params['verify']) { - this.subs.push(this.userManagementService.getUserInfo(false).subscribe(user => { + this.subs.push(this.userManagementService.getUserInfo().subscribe(user => { this.user = user; if (this.user) { this.subs.push(this.userRegistryService.getInvitation(params['verify']).subscribe(verification => { diff --git a/services/user-management.service.ts b/services/user-management.service.ts index d6a1ccd5..d7b640eb 100644 --- a/services/user-management.service.ts +++ b/services/user-management.service.ts @@ -16,22 +16,11 @@ export class UserManagementService { private readonly getUserInfoSubject: AdvancedAsyncSubject = new AdvancedAsyncSubject(); public fixRedirectURL: string = null; private redirectUrl: string = null; - private readonly promise: Promise; private subscription; private readonly routerSubscription; - constructor(private http: HttpClient, private router: Router) { - this.promise = new Promise((resolve => { - this.updateUserInfo(resolve); - })); - this.routerSubscription = this.router.events.subscribe(event => { - if (event instanceof NavigationEnd) { - const token = COOKIE.getCookie('AccessToken'); - if (!token && this.getUserInfoSubject.getValue() !== null) { - this.getUserInfoSubject.next(null); - } - } - }); + constructor(private http: HttpClient) { + this.updateUserInfo(); } clearSubscriptions() { @@ -43,12 +32,12 @@ export class UserManagementService { } } - public getUserInfo(subject: boolean = true): Observable { - if (subject) { - return this.getUserInfoSubject.asObservable(); - } else { - return from(this.getUserInfoAsync()); - } + public get user(): User { + return this.getUserInfoSubject.getValue(); + } + + public getUserInfo(): Observable { + return this.getUserInfoSubject.asObservable(); } public updateUserInfo(resolve: Function = null) { @@ -67,14 +56,6 @@ export class UserManagementService { }); } - private async getUserInfoAsync(): Promise { - await this.promise; - if (this.subscription) { - this.subscription.unsubscribe(); - } - return this.getUserInfoSubject.getValue(); - } - private parseUserInfo(info: any) { const user: User = new User(); user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub; @@ -144,6 +125,7 @@ export class UserManagementService { public logout() { this.setRedirectUrl(); Session.removeUser(); + this.getUserInfoSubject.next(null); window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl; } }