Add redirect url in login/logout. Add new userInfo method in getUserInfo

This commit is contained in:
Konstantinos Triantafyllou 2021-11-05 13:29:54 +02:00
parent 104c54a3e8
commit 0aa44646b1
2 changed files with 30 additions and 26 deletions

View File

@ -1,5 +1,3 @@
import {StringUtils} from "../../utils/string-utils.class";
export class User {
email: string;
firstname: string;
@ -15,8 +13,6 @@ export class User {
export class Session {
public static removeUser() {
COOKIE.deleteCookie(COOKIE.cookieName_id);
//COOKIE.deleteCookie("openAIRESession");
COOKIE.deleteCookie("openAIREUser");
}
public static isLoggedIn(): boolean {

View File

@ -6,14 +6,16 @@ import {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment";
import {StringUtils} from "../utils/string-utils.class";
import {CustomOptions} from "./servicesUtils/customOptions.class";
@Injectable({
providedIn: 'root'
})
export class UserManagementService{
export class UserManagementService {
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
public fixRedirectURL: string = null;
private redirectUrl: string = null;
private readonly promise: Promise<User>;
sub;
routeSub;
@ -32,12 +34,12 @@ export class UserManagementService{
}
});
}
clearSubscriptions() {
if(this.routeSub){
if (this.routeSub) {
this.routeSub.unsubscribe();
}
if(this.sub){
if (this.sub) {
this.sub.unsubscribe();
}
}
@ -51,32 +53,36 @@ export class UserManagementService{
}
public updateUserInfo(resolve: Function = null) {
/** @deprecated **/
const token = COOKIE.getCookie('AccessToken');
if (!token) {
if (!token && properties.userInfoUrl.includes("accessToken")) {
this.getUserInfoSubject.next(null);
if(resolve) {
if (resolve) {
resolve();
}
} else {
this.sub = this.http.get<User>(properties.userInfoUrl + token).pipe(map(userInfo => {
let userInfoUrl = (properties.userInfoUrl.includes("accessToken")?(properties.userInfoUrl + token):properties.userInfoUrl);
this.sub = this.http.get<User>(userInfoUrl, CustomOptions.registryOptions()).pipe(map(userInfo => {
return this.parseUserInfo(userInfo);
})).subscribe(user => {
this.getUserInfoSubject.next(user);
if(resolve) {
if (resolve) {
resolve();
}
}, error => {
this.getUserInfoSubject.next(null);
if(resolve) {
resolve();
}
});
if(this.getUserInfoSubject.value) {
this.getUserInfoSubject.next(null);
}
if (resolve) {
resolve();
}
});
}
}
private async getUserInfoAsync(): Promise<User> {
await this.promise;
if(this.sub){
if (this.sub) {
this.sub.unsubscribe();
}
return this.getUserInfoSubject.getValue();
@ -105,7 +111,7 @@ export class UserManagementService{
info.roles.forEach(role => {
user.role.push(role);
});
}else{
} else {
if (info.edu_person_entitlements) {
user.role = info.edu_person_entitlements;
}
@ -115,7 +121,7 @@ export class UserManagementService{
}
public setRedirectUrl(url: string = null) {
if(url) {
if (url) {
let parts = url.split('?');
let path = properties.baseLink + parts[0];
let params = null;
@ -123,34 +129,36 @@ export class UserManagementService{
params = parts[1];
}
let hash = path.indexOf("#");
let fragment = (hash !== -1)?path.slice(hash + 1):null;
if(fragment) {
let fragment = (hash !== -1) ? path.slice(hash + 1) : null;
if (fragment) {
path = path.slice(0, hash);
} else {
fragment = "";
}
if(!path.includes('/reload')) {
if (!path.includes('/reload')) {
Session.setReloadUrl(location.protocol + "//" + location.host, path, params, fragment);
}
this.redirectUrl = StringUtils.URIEncode(location.protocol + "//" + location.host + this.fixRedirectURL);
} else {
this.redirectUrl = StringUtils.URIEncode(location.href);
Session.setReloadUrl(location.protocol + "//" + location.host, location.pathname, location.search, location.hash);
}
}
public login() {
if(this.fixRedirectURL) {
if (this.fixRedirectURL) {
this.setRedirectUrl(this.fixRedirectURL);
} else {
this.setRedirectUrl();
}
window.location.href = properties.loginUrl;
window.location.href = properties.loginUrl + "?redirect=" + this.redirectUrl;
}
public logout() {
this.setRedirectUrl();
Session.removeUser();
if (properties.logoutUrl.includes('openid_logout')) {
window.location.href = properties.logoutUrl;
window.location.href = properties.logoutUrl + "?redirect=" + this.redirectUrl;
} else {
window.location.href = properties.logoutUrl + StringUtils.URIEncode(location.href);
}