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:
parent
6d84a2eb58
commit
1c4551c408
|
@ -22,7 +22,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanActivateChild {
|
|||
|
||||
check(community: string, path: string): Observable<boolean> | 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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -22,7 +22,7 @@ export class AdminLoginGuard implements CanActivate, CanActivateChild {
|
|||
|
||||
check(data: Data, 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;
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ export class ClaimsCuratorGuard implements CanActivate {
|
|||
|
||||
check(path: string): Observable<boolean> | 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;
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ export class LoginGuard implements CanActivate, CanLoad, CanActivateChild {
|
|||
}
|
||||
|
||||
check(path: string): Observable<boolean> | boolean {
|
||||
return this.userManagementService.getUserInfo(false).pipe(map(user => {
|
||||
return this.userManagementService.getUserInfo().pipe(map(user => {
|
||||
return user !== null;
|
||||
}),tap(isLoggedIn => {
|
||||
if(!isLoggedIn) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}));
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 => {
|
||||
|
|
|
@ -16,22 +16,11 @@ export class UserManagementService {
|
|||
private readonly getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>();
|
||||
public fixRedirectURL: string = null;
|
||||
private redirectUrl: string = null;
|
||||
private readonly promise: Promise<User>;
|
||||
private subscription;
|
||||
private readonly routerSubscription;
|
||||
|
||||
constructor(private http: HttpClient, private router: Router) {
|
||||
this.promise = new Promise<any>((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<User> {
|
||||
if (subject) {
|
||||
return this.getUserInfoSubject.asObservable();
|
||||
} else {
|
||||
return from(this.getUserInfoAsync());
|
||||
}
|
||||
public get user(): User {
|
||||
return this.getUserInfoSubject.getValue();
|
||||
}
|
||||
|
||||
public getUserInfo(): Observable<User> {
|
||||
return this.getUserInfoSubject.asObservable();
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue