Remove promise from user management service. Change user component in order to update user-info in case of session expired

This commit is contained in:
Konstantinos Triantafyllou 2022-09-08 16:43:59 +03:00
parent 6d84a2eb58
commit 1c4551c408
8 changed files with 18 additions and 36 deletions

View File

@ -22,7 +22,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanActivateChild {
check(community: string, path: string): Observable<boolean> | boolean { check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; 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 (user) {
if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) { if (Session.isPortalAdministrator(user) || Session.isCommunityCurator(user) || Session.isManager('community', community, user)) {
return of(true); return of(true);

View File

@ -30,7 +30,7 @@ export class ConnectSubscriberGuard implements CanActivate, CanActivateChild {
} else { } else {
community = ConnectHelper.getCommunityFromDomain(properties.domain); 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 (user) {
if (Session.isSubscribedTo('community', community, user)) { if (Session.isSubscribedTo('community', community, user)) {
return of(true); return of(true);

View File

@ -22,7 +22,7 @@ export class AdminLoginGuard implements CanActivate, CanActivateChild {
check(data: Data, path: string): Observable<boolean> { check(data: Data, path: string): Observable<boolean> {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
return this.userManagementService.getUserInfo(false).pipe(map(user => { return this.userManagementService.getUserInfo().pipe(map(user => {
if (user) { if (user) {
errorCode = LoginErrorCodes.NOT_ADMIN; errorCode = LoginErrorCodes.NOT_ADMIN;
} }

View File

@ -15,7 +15,7 @@ export class ClaimsCuratorGuard implements CanActivate {
check(path: string): Observable<boolean> | boolean { check(path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
return this.userManagementService.getUserInfo(false).pipe(map(user => { return this.userManagementService.getUserInfo().pipe(map(user => {
if (user) { if (user) {
errorCode = LoginErrorCodes.NOT_ADMIN; errorCode = LoginErrorCodes.NOT_ADMIN;
} }

View File

@ -22,7 +22,7 @@ export class LoginGuard implements CanActivate, CanLoad, CanActivateChild {
} }
check(path: string): Observable<boolean> | boolean { check(path: string): Observable<boolean> | boolean {
return this.userManagementService.getUserInfo(false).pipe(map(user => { return this.userManagementService.getUserInfo().pipe(map(user => {
return user !== null; return user !== null;
}),tap(isLoggedIn => { }),tap(isLoggedIn => {
if(!isLoggedIn) { if(!isLoggedIn) {

View File

@ -43,8 +43,8 @@ export class UserComponent {
this.loginUrl = this.properties.loginUrl; this.loginUrl = this.properties.loginUrl;
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
this.server = false; this.server = false;
this.subscriptions.push(this.userManagementsService.getUserInfo(false).subscribe(user => { this.userManagementsService.updateUserInfo(() => {
this.user = user; this.user = this.userManagementsService.user;
this.loggedIn = !!this.user; this.loggedIn = !!this.user;
this.errorMessage = ""; this.errorMessage = "";
this.loading = true; this.loading = true;
@ -58,7 +58,7 @@ export class UserComponent {
this.loading = false; this.loading = false;
} }
})); }));
})); });
} }
} }

View File

@ -103,7 +103,7 @@ export class RoleVerificationComponent implements OnInit, OnDestroy, AfterViewIn
ngAfterViewInit() { ngAfterViewInit() {
this.subs.push(this.route.queryParams.subscribe(params => { this.subs.push(this.route.queryParams.subscribe(params => {
if (params && params['verify']) { if (params && params['verify']) {
this.subs.push(this.userManagementService.getUserInfo(false).subscribe(user => { this.subs.push(this.userManagementService.getUserInfo().subscribe(user => {
this.user = user; this.user = user;
if (this.user) { if (this.user) {
this.subs.push(this.userRegistryService.getInvitation(params['verify']).subscribe(verification => { this.subs.push(this.userRegistryService.getInvitation(params['verify']).subscribe(verification => {

View File

@ -16,22 +16,11 @@ export class UserManagementService {
private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>(); private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>();
public fixRedirectURL: string = null; public fixRedirectURL: string = null;
private redirectUrl: string = null; private redirectUrl: string = null;
private readonly promise: Promise<User>;
private subscription; private subscription;
private readonly routerSubscription; private readonly routerSubscription;
constructor(private http: HttpClient, private router: Router) { constructor(private http: HttpClient) {
this.promise = new Promise<any>((resolve => { this.updateUserInfo();
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);
}
}
});
} }
clearSubscriptions() { clearSubscriptions() {
@ -43,12 +32,12 @@ export class UserManagementService {
} }
} }
public getUserInfo(subject: boolean = true): Observable<User> { public get user(): User {
if (subject) { return this.getUserInfoSubject.getValue();
return this.getUserInfoSubject.asObservable(); }
} else {
return from(this.getUserInfoAsync()); public getUserInfo(): Observable<User> {
} return this.getUserInfoSubject.asObservable();
} }
public updateUserInfo(resolve: Function = null) { public updateUserInfo(resolve: Function = null) {
@ -67,14 +56,6 @@ export class UserManagementService {
}); });
} }
private async getUserInfoAsync(): Promise<User> {
await this.promise;
if (this.subscription) {
this.subscription.unsubscribe();
}
return this.getUserInfoSubject.getValue();
}
private parseUserInfo(info: any) { private parseUserInfo(info: any) {
const user: User = new User(); const user: User = new User();
user.id = (info.sub && info.sub.indexOf('@')) ? info.sub.substring(0, info.sub.indexOf('@')) : info.sub; 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() { public logout() {
this.setRedirectUrl(); this.setRedirectUrl();
Session.removeUser(); Session.removeUser();
this.getUserInfoSubject.next(null);
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl; window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} }
} }