[Library]: Change userInfo: Make request only if subject is null(and token exists). After every navigation check token existance
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@57788 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
af9b7ac0ea
commit
c6186bb738
|
@ -1,4 +1,4 @@
|
|||
import {Component, ElementRef, Input} from '@angular/core';
|
||||
import {Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {Location} from '@angular/common';
|
||||
import {User,Session} from './utils/helper.class';
|
||||
|
@ -70,7 +70,7 @@ import{EnvProperties} from '../utils/properties/env-properties';
|
|||
`
|
||||
})
|
||||
|
||||
export class UserMiniComponent {
|
||||
export class UserMiniComponent implements OnInit, OnChanges{
|
||||
@Input() user: User;
|
||||
public loggedIn: boolean = false;
|
||||
public isAuthorized: boolean = false;
|
||||
|
@ -98,6 +98,13 @@ export class UserMiniComponent {
|
|||
|
||||
|
||||
}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges): void {
|
||||
if(changes.user) {
|
||||
this.initialize();
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(){
|
||||
if(this.sub) {
|
||||
this.sub.unsubscribe();
|
||||
|
|
|
@ -1,26 +1,45 @@
|
|||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable, of} from "rxjs";
|
||||
import {BehaviorSubject, Observable, of} from "rxjs";
|
||||
import {COOKIE, User} from "../login/utils/helper.class";
|
||||
import {catchError, map, timeout} from "rxjs/operators";
|
||||
import {NavigationEnd, Router} from "@angular/router";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserManagementService {
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
||||
|
||||
constructor(private http: HttpClient,
|
||||
private router: Router) {
|
||||
this.router.events.subscribe( event => {
|
||||
if(event instanceof NavigationEnd) {
|
||||
const token = COOKIE.getCookie('AccessToken');
|
||||
if(!token && this.getUserInfoSubject.getValue() !== null) {
|
||||
this.getUserInfoSubject.next(null);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public getUserInfo(url: string): Observable<User> {
|
||||
const token = COOKIE.getCookie('AccessToken');
|
||||
if(!token) {
|
||||
return of(null);
|
||||
} else return this.http.get<User>(url + token).pipe(map(userInfo => {
|
||||
return this.parseUserInfo(userInfo);
|
||||
})).pipe(timeout(2000), catchError(() => {
|
||||
return of(null);
|
||||
}));
|
||||
this.getUserInfoSubject.next(null);
|
||||
} else {
|
||||
if(this.getUserInfoSubject.getValue() === null) {
|
||||
this.http.get<User>(url + token).pipe(map(userInfo => {
|
||||
return this.parseUserInfo(userInfo);
|
||||
})).pipe(timeout(2000), catchError(() => {
|
||||
return of(null);
|
||||
})).subscribe(user => {
|
||||
this.getUserInfoSubject.next(user);
|
||||
});
|
||||
}
|
||||
}
|
||||
return this.getUserInfoSubject.asObservable();
|
||||
}
|
||||
|
||||
private parseUserInfo(info: any) {
|
||||
|
|
Loading…
Reference in New Issue