removed openAIREUser cookie usage

This commit is contained in:
Konstantinos Spyrou 2021-07-19 11:40:18 +00:00
parent 64b5412f5e
commit cbff7a55ee
2 changed files with 22 additions and 24 deletions

View File

@ -10,12 +10,7 @@ export class AuthGuardService implements CanActivate, CanLoad {
constructor (private authenticationService: AuthenticationService, private router: Router) {} constructor (private authenticationService: AuthenticationService, private router: Router) {}
canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivate (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if ( this.authenticationService.getIsUserLoggedIn() ) { return true; }
if ( (getCookie('openAIREUser') !== null) &&
(getCookie('openAIREUser') !== '') &&
this.authenticationService.getIsUserLoggedIn() ) { return true; }
if ( (getCookie('openAIREUser') !== null) && (getCookie('openAIREUser') !== '') ) { return true; }
/* If no cookie was found, clear the app's session. /* If no cookie was found, clear the app's session.
The user may have logged out using another OpenAIRE portal */ The user may have logged out using another OpenAIRE portal */

View File

@ -19,6 +19,8 @@ export class AuthenticationService {
private _storage: Storage = sessionStorage; private _storage: Storage = sessionStorage;
private cookie: string = null;
isLoggedIn: boolean = false; isLoggedIn: boolean = false;
public loginWithState() { public loginWithState() {
@ -37,7 +39,6 @@ export class AuthenticationService {
} }
public logout() { public logout() {
deleteCookie('openAIREUser');
deleteCookie('AccessToken'); deleteCookie('AccessToken');
sessionStorage.clear(); sessionStorage.clear();
this.isLoggedIn = false; this.isLoggedIn = false;
@ -50,22 +51,24 @@ export class AuthenticationService {
} }
public tryLogin() { public tryLogin() {
if ( getCookie('openAIREUser') && (getCookie('openAIREUser') !== '') ) { this.cookie = getCookie('AccessToken');
console.log(`I got the cookie!`); if (this.cookie && this.cookie !== '') {
console.log(`in tryLogin -> document.cookie is: ${document.cookie.toString()}`); // console.log(`I got the cookie!`);
// console.log(`in tryLogin -> document.cookie is: ${document.cookie.toString()}`);
/* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */ /* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */
setInterval(() => { setInterval(() => {
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe( this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
userInfo => { userInfo => {
console.log('User is still logged in'); // console.log('User is still logged in');
console.log(userInfo); // console.log(userInfo);
this.isLoggedIn = true; this.isLoggedIn = true;
}, },
() => { () => {
this.logout(); this.logout();
}, },
() => { () => {
if ( !getCookie('openAIREUser') || (getCookie('openAIREUser') === '') ) { this.cookie = getCookie('AccessToken');
if ( !this.cookie || this.cookie === '') {
this.logout(); this.logout();
} }
} }
@ -75,22 +78,22 @@ export class AuthenticationService {
}, 1000 * 60 * 5); }, 1000 * 60 * 5);
if (!this.getIsUserLoggedIn()) { if (!this.getIsUserLoggedIn()) {
console.log(`session.name wasn't found --> logging in via repo-service!`); // console.log(`session.name wasn't found --> logging in via repo-service!`);
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe( this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
userInfo => { userInfo => {
console.log(userInfo); // console.log(userInfo);
sessionStorage.setItem('name', userInfo['name']); sessionStorage.setItem('name', userInfo['name']);
sessionStorage.setItem('email', userInfo['email'].trim()); sessionStorage.setItem('email', userInfo['email'].trim());
sessionStorage.setItem('role', userInfo['role']); sessionStorage.setItem('role', userInfo['role']);
this.isLoggedIn = true; this.isLoggedIn = true;
console.log(`the current user is: ${sessionStorage.getItem('name')}, // console.log(`the current user is: ${sessionStorage.getItem('name')},
${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`); // ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
}, },
error => { error => {
sessionStorage.clear(); sessionStorage.clear();
console.log('Error!'); console.log('Error!');
console.log(error); console.log(error);
deleteCookie('openAIREUser'); deleteCookie('AccessToken');
deleteCookie('AccessToken'); deleteCookie('AccessToken');
this.isLoggedIn = false; this.isLoggedIn = false;
this.router.navigate(['/home']); this.router.navigate(['/home']);
@ -101,7 +104,7 @@ export class AuthenticationService {
sessionStorage.removeItem('state.location'); sessionStorage.removeItem('state.location');
console.log(`tried to login - returning to state: ${state}`); console.log(`tried to login - returning to state: ${state}`);
if ( !this.getIsUserLoggedIn() ) { if ( !this.getIsUserLoggedIn() ) {
console.log('user hasn\'t logged in yet -- going to home'); // console.log('user hasn\'t logged in yet -- going to home');
this.router.navigate(['/home']); this.router.navigate(['/home']);
} else { } else {
this.router.navigate([state]); this.router.navigate([state]);
@ -111,21 +114,21 @@ export class AuthenticationService {
); );
} else { } else {
this.isLoggedIn = true; this.isLoggedIn = true;
console.log(`the current user is: ${sessionStorage.getItem('name')}, // console.log(`the current user is: ${sessionStorage.getItem('name')},
${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`); // ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
if (this.redirectUrl) { if (this.redirectUrl) {
const url = this.redirectUrl; const url = this.redirectUrl;
this.redirectUrl = null; this.redirectUrl = null;
this.router.navigate([url]); this.router.navigate([url]);
console.log('route is', url); // console.log('route is', url);
} }
} }
} }
} }
public getIsUserLoggedIn() { public getIsUserLoggedIn() {
this.isLoggedIn = (getCookie('openAIREUser') && (getCookie('openAIREUser') !== '') && (sessionStorage.getItem('email') !== null ) ); // todo: probably not all of them are needed
return this.isLoggedIn; return this.isLoggedIn && this.cookie && this.cookie !== '' && sessionStorage.getItem('email') !== null;
} }
public getUserName() { public getUserName() {