Don't save user in localstorage - use always cookie | remove check for expiration cookie - expires when cookie expires in 4 hours
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@51641 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
fa56fe4d30
commit
815c5cd3ee
|
@ -17,7 +17,6 @@ export class ConnectHelper{
|
|||
return domain;
|
||||
}
|
||||
public static isProduction(domain:string):boolean{
|
||||
console.log("Domain:"+domain);
|
||||
if(domain.indexOf(".di.uoa.gr")!=-1){
|
||||
return false;
|
||||
}else if(domain.indexOf(".openaire.eu")!=-1){
|
||||
|
|
|
@ -21,78 +21,104 @@ export class Session{
|
|||
// }
|
||||
public static removeUser() {
|
||||
if(Session.isLoggedIn()){
|
||||
localStorage.removeItem("user");
|
||||
// localStorage.removeItem("user");
|
||||
COOKIE.deleteCookie(COOKIE.cookieName_id)
|
||||
COOKIE.deleteCookie(COOKIE.cookieName_u);
|
||||
}
|
||||
}
|
||||
public static getUser():User {
|
||||
if(Session.isLoggedIn()){
|
||||
return JSON.parse(localStorage.getItem("user"));
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
// if(Session.isLoggedIn()){
|
||||
// return JSON.parse(localStorage.getItem("user"));
|
||||
// }else{
|
||||
// return null;
|
||||
// }
|
||||
return this.getUserFromCookie();
|
||||
}
|
||||
// public static isLoggedIn(): boolean {
|
||||
// var loggedIn:boolean = false;
|
||||
// var user:User = null;
|
||||
// var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
||||
// if( typeof localStorage !== 'undefined') {
|
||||
// if(localStorage.getItem("user") || (cookie != null && cookie != "")) {
|
||||
// user = JSON.parse(localStorage.getItem("user"));
|
||||
// if(user && (user.fullname != null || user.lastname !=null || user.firstname !=null)){
|
||||
// loggedIn = true;
|
||||
// }else if(cookie != null && this.getUserFromCookie()!= null){
|
||||
// loggedIn = true
|
||||
// }else{
|
||||
// loggedIn = false;
|
||||
// }
|
||||
// }else{
|
||||
// if(cookie != null && this.getUserFromCookie()!= null){
|
||||
// loggedIn = true
|
||||
// }else{
|
||||
// loggedIn = false;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }else{
|
||||
// loggedIn = false;
|
||||
// }
|
||||
// return loggedIn;
|
||||
// }
|
||||
public static isLoggedIn(): boolean {
|
||||
var loggedIn:boolean = false;
|
||||
var user:User = null;
|
||||
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
||||
if( typeof localStorage !== 'undefined') {
|
||||
if(localStorage.getItem("user") || (cookie != null && cookie != "")) {
|
||||
user = JSON.parse(localStorage.getItem("user"));
|
||||
if(user && (user.fullname != null || user.lastname !=null || user.firstname !=null)){
|
||||
loggedIn = true;
|
||||
}else if(cookie != null && this.getUserFromCookie()!= null){
|
||||
loggedIn = true
|
||||
}else{
|
||||
loggedIn = false;
|
||||
}
|
||||
}else{
|
||||
if(cookie != null && this.getUserFromCookie()!= null){
|
||||
loggedIn = true
|
||||
}else{
|
||||
loggedIn = false;
|
||||
}
|
||||
|
||||
}
|
||||
if(cookie == null ||cookie == ""){
|
||||
return false;
|
||||
}else{
|
||||
loggedIn = false;
|
||||
var user:User = this.getUserFromCookie();
|
||||
if(user ==null){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return loggedIn;
|
||||
|
||||
}
|
||||
public static getUserFromCookie():User{
|
||||
var cookie= COOKIE.getCookie(COOKIE.cookieName_u);
|
||||
if(cookie != null){
|
||||
var user:User = MyJWT.parseUserInfo(cookie);
|
||||
if( typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem("user", JSON.stringify(user));
|
||||
// if( typeof localStorage !== 'undefined') {
|
||||
// localStorage.setItem("user", JSON.stringify(user));
|
||||
if(user && user.email){
|
||||
// COOKIE.deleteCookie(COOKIE.cookieName_u); // delete cookie to avoid transfer through requests
|
||||
return user;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
// }else{
|
||||
// return null;
|
||||
// }
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static getUserJwt():string {
|
||||
if(Session.isLoggedIn()){
|
||||
return Session.getUser().jwt;
|
||||
}else{
|
||||
return null;
|
||||
var user:User = this.getUserFromCookie();
|
||||
if(user!=null){
|
||||
return user.jwt;
|
||||
}
|
||||
return null;
|
||||
// if(Session.isLoggedIn()){
|
||||
// return Session.getUser().jwt;
|
||||
// }else{
|
||||
// return null;
|
||||
// }
|
||||
|
||||
}
|
||||
public static getUserEmail():string {
|
||||
if(Session.isLoggedIn()){
|
||||
return Session.getUser().email;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
var user:User = this.getUserFromCookie();
|
||||
if(user!=null){
|
||||
return user.email;
|
||||
}
|
||||
return null;
|
||||
// if(Session.isLoggedIn()){
|
||||
// return Session.getUser().email;
|
||||
// }else{
|
||||
// return null;
|
||||
// }
|
||||
|
||||
}
|
||||
public static getCurrentUrl():string {
|
||||
|
@ -131,18 +157,21 @@ export class Session{
|
|||
}
|
||||
public static isClaimsCurator():boolean {
|
||||
var isAuthorized = false;
|
||||
if(Session.isLoggedIn()){
|
||||
var user:User = this.getUserFromCookie();
|
||||
if(user!=null){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
var claimRoles = ["urn:geant:openaire.eu:group:Curator+-+Claim#aai.openaire.eu"]
|
||||
for (var i = 0; i < claimRoles.length; i++) {
|
||||
if ((Session.getUser().role).indexOf(claimRoles[i]) > -1) {
|
||||
if ((user.role).indexOf(claimRoles[i]) > -1) {
|
||||
isAuthorized = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (isAuthorized);
|
||||
}
|
||||
return (isAuthorized);
|
||||
|
||||
}
|
||||
public static isCommunityCurator():boolean {
|
||||
var isAuthorized = false;
|
||||
|
@ -191,25 +220,28 @@ export class Session{
|
|||
return (isRegisteredUser);
|
||||
}
|
||||
public static isUserValid() {
|
||||
if(Session.isLoggedIn()){
|
||||
var expires = Session.getUser().expirationDate;
|
||||
var now = new Date().getTime() / 1000;
|
||||
// console.log(" is still valid ? "+(now +0 < expires) +" Remaining:"+ (expires - (now+0))+ " now is:"+now + "expires at:"+expires);
|
||||
return now +0 < expires;
|
||||
}
|
||||
// if(Session.isLoggedIn()){
|
||||
var user:User = Session.getUser();
|
||||
if(user!=null){
|
||||
// var expires = user.expirationDate;
|
||||
// var now = new Date().getTime() / 1000;
|
||||
// console.log(" is still valid ? "+(now +0 < expires) +" Remaining:"+ (expires - (now+0))+ " now is:"+now + "expires at:"+expires);
|
||||
// return now +0 < expires;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static isValidAndRemove() {
|
||||
if(Session.isLoggedIn()){
|
||||
// if(Session.isLoggedIn()){
|
||||
if(!Session.isUserValid()){
|
||||
Session.removeUser();
|
||||
return false;
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
// }else{
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -259,8 +291,8 @@ export class MyJWT{
|
|||
// console.log(user.role)
|
||||
user.jwt = data;
|
||||
user.expirationDate = info.exp;
|
||||
localStorage.setItem("user", JSON.stringify(user));
|
||||
console.log(user)
|
||||
// localStorage.setItem("user", JSON.stringify(user));
|
||||
// console.log(user)
|
||||
return user;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue