uoa-repository-manager-ui/src/app/services/authentication.service.ts

159 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-07-09 12:11:25 +02:00
import { Injectable } from '@angular/core';
2023-12-21 13:40:07 +01:00
import { Router } from '@angular/router';
2021-07-09 12:11:25 +02:00
import { HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment';
import { deleteCookie, getCookie } from '../domain/utils';
2022-04-06 12:10:42 +02:00
import { BehaviorSubject } from 'rxjs';
2021-07-09 12:11:25 +02:00
@Injectable()
export class AuthenticationService {
2023-12-21 13:40:07 +01:00
constructor(private router: Router,
2021-07-09 12:11:25 +02:00
private http: HttpClient) {}
private apiUrl: string = environment.API_ENDPOINT;
private loginUrl = environment.API_ENDPOINT + '/openid_connect_login';
// store the URL so we can redirect after logging in
public redirectUrl: string;
2021-07-19 13:40:18 +02:00
private cookie: string = null;
2022-04-06 12:10:42 +02:00
public isLoggedIn_ = new BehaviorSubject(false);
public get isLoggedIn() {
return this.isLoggedIn_;
2022-04-06 12:10:42 +02:00
}
2021-07-09 12:11:25 +02:00
public loginWithState() {
console.log(`logging in with state. Current url is: ${this.router.url}`);
if (this.redirectUrl) {
const url = this.redirectUrl;
this.redirectUrl = null;
sessionStorage.setItem('state.location', url);
} else {
/*sessionStorage.setItem("state.location", this.router.url);*/
sessionStorage.setItem('state.location', '/join');
}
console.log('redirect location', sessionStorage.getItem('state.location'));
window.location.href = this.loginUrl;
}
public logout() {
deleteCookie('AccessToken');
sessionStorage.clear();
2022-04-06 12:10:42 +02:00
this.isLoggedIn_.next(false);
2021-07-09 12:11:25 +02:00
console.log('logging out, calling:');
console.log(`${this.apiUrl}/openid_logout`);
/*window.location.href = `${this.apiUrl}/openid_logout`;*/
2021-07-22 10:36:29 +02:00
window.location.href = `${environment.AAI_LOGOUT + window.location.origin + this.apiUrl}/openid_logout`;
2021-07-09 12:11:25 +02:00
}
public tryLogin() {
2021-07-19 13:40:18 +02:00
this.cookie = getCookie('AccessToken');
if (this.cookie && this.cookie !== '') {
// console.log(`I got the cookie!`);
// console.log(`in tryLogin -> document.cookie is: ${document.cookie.toString()}`);
2021-07-09 12:11:25 +02:00
/* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTDOWN */
setInterval(() => {
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
userInfo => {
2021-07-19 13:40:18 +02:00
// console.log('User is still logged in');
// console.log(userInfo);
2022-04-06 12:10:42 +02:00
this.isLoggedIn_.next(true);
2021-07-09 12:11:25 +02:00
},
() => {
this.logout();
},
() => {
2021-07-19 13:40:18 +02:00
this.cookie = getCookie('AccessToken');
if ( !this.cookie || this.cookie === '') {
2021-07-09 12:11:25 +02:00
this.logout();
}
}
);
/*this.redirectUrl = window.location.pathname;
this.loginWithState();*/
}, 1000 * 60 * 5);
if (!this.getIsUserLoggedIn()) {
2021-07-19 13:40:18 +02:00
// console.log(`session.name wasn't found --> logging in via repo-service!`);
2021-07-09 12:11:25 +02:00
this.http.get(this.apiUrl + '/user/login', { withCredentials: true }).subscribe(
userInfo => {
2021-07-19 13:40:18 +02:00
// console.log(userInfo);
2021-07-09 12:11:25 +02:00
sessionStorage.setItem('name', userInfo['name']);
sessionStorage.setItem('email', userInfo['email'].trim());
sessionStorage.setItem('role', userInfo['role']);
2022-04-06 12:10:42 +02:00
this.isLoggedIn_.next(true);
2021-07-19 13:40:18 +02:00
// console.log(`the current user is: ${sessionStorage.getItem('name')},
// ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
2021-07-09 12:11:25 +02:00
},
error => {
sessionStorage.clear();
console.log('Error!');
console.log(error);
2021-07-19 13:40:18 +02:00
deleteCookie('AccessToken');
2021-07-09 12:11:25 +02:00
deleteCookie('AccessToken');
2022-04-06 12:10:42 +02:00
this.isLoggedIn_.next(false);
2021-07-09 12:11:25 +02:00
this.router.navigate(['/home']);
},
() => {
if ( sessionStorage.getItem('state.location') ) {
const state = sessionStorage.getItem('state.location');
sessionStorage.removeItem('state.location');
console.log(`tried to login - returning to state: ${state}`);
if ( !this.getIsUserLoggedIn() ) {
2023-12-21 13:40:07 +01:00
// console.log('user hasn't logged in yet -- redirecting to home');
2021-07-09 12:11:25 +02:00
this.router.navigate(['/home']);
} else {
this.router.navigate([state]);
}
}
}
);
} else {
2022-04-06 12:10:42 +02:00
this.isLoggedIn_.next(true);
2021-07-19 13:40:18 +02:00
// console.log(`the current user is: ${sessionStorage.getItem('name')},
// ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
2021-07-09 12:11:25 +02:00
if (this.redirectUrl) {
const url = this.redirectUrl;
this.redirectUrl = null;
this.router.navigate([url]);
2021-07-19 13:40:18 +02:00
// console.log('route is', url);
2021-07-09 12:11:25 +02:00
}
}
}
}
public getIsUserLoggedIn() {
2021-07-19 13:40:18 +02:00
// todo: probably not all of them are needed
2022-04-06 12:10:42 +02:00
return this.isLoggedIn_.value && this.cookie && this.cookie !== '' && sessionStorage.getItem('email') !== null;
2021-07-09 12:11:25 +02:00
}
public getUserName() {
2022-04-06 12:10:42 +02:00
if (this.isLoggedIn_.value) {
2021-07-09 12:11:25 +02:00
return sessionStorage.getItem('name');
} else {
return '';
}
}
public getUserEmail() {
if (this.getIsUserLoggedIn()) {
return sessionStorage.getItem('email');
} else {
return '';
}
}
public getUserRole() {
2022-04-06 12:10:42 +02:00
if (this.isLoggedIn_.value) {
2021-07-09 12:11:25 +02:00
return sessionStorage.getItem('role');
} else {
return '';
}
}
}