1
|
import { Injectable } from "@angular/core";
|
2
|
import { ActivatedRoute, Router } from '@angular/router';
|
3
|
import { apiUrl, appBaseUrl } from '../domain/tempAPI';
|
4
|
import { deleteCookie, getCookie } from '../domain/utils';
|
5
|
import { Http } from '@angular/http';
|
6
|
|
7
|
@Injectable()
|
8
|
export class AuthenticationService {
|
9
|
|
10
|
constructor(private route: ActivatedRoute,
|
11
|
private router: Router,
|
12
|
private http: Http) {}
|
13
|
|
14
|
/*private apiUrl : string = apiUrl;*/
|
15
|
/*private loginUrl : string = `${this.apiUrl}/openid_connect_login`;*/
|
16
|
|
17
|
private apiUrl : string = process.env.API_ENDPOINT;
|
18
|
private loginUrl: string = process.env.AAI_ENDPOINT;
|
19
|
private baseUrl: string = process.env.BASE_URL;
|
20
|
|
21
|
// store the URL so we can redirect after logging in
|
22
|
public redirectUrl: string;
|
23
|
|
24
|
private _storage: Storage = sessionStorage;
|
25
|
|
26
|
|
27
|
isLoggedIn: boolean = false;
|
28
|
|
29
|
public loginWithState() {
|
30
|
console.log(`logging in with state. Current url is: ${this.router.url}`);
|
31
|
sessionStorage.setItem("state.location", this.router.url);
|
32
|
window.location.href = this.loginUrl;
|
33
|
}
|
34
|
|
35
|
public logout() {
|
36
|
deleteCookie('currentUser');
|
37
|
sessionStorage.removeItem('name');
|
38
|
sessionStorage.removeItem('email');
|
39
|
sessionStorage.removeItem('role');
|
40
|
this.isLoggedIn = false;
|
41
|
|
42
|
console.log('logging out, going to:');
|
43
|
console.log(`https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.baseUrl}`);
|
44
|
window.location.href = `https://aai.openaire.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo=${this.baseUrl}`;
|
45
|
}
|
46
|
|
47
|
public tryLogin() {
|
48
|
if(getCookie('currentUser')) {
|
49
|
console.log(`I got the cookie!`);
|
50
|
/* SETTING INTERVAL TO REFRESH SESSION TIMEOUT COUNTD */
|
51
|
setInterval(() => {
|
52
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
53
|
userInfo => {
|
54
|
console.log("User is still logged in");
|
55
|
console.log(userInfo.json());
|
56
|
this.isLoggedIn = true;
|
57
|
},
|
58
|
() => {
|
59
|
sessionStorage.removeItem('name');
|
60
|
sessionStorage.removeItem('email');
|
61
|
sessionStorage.removeItem('role');
|
62
|
deleteCookie('currentUser');
|
63
|
this.isLoggedIn = false;
|
64
|
}
|
65
|
);
|
66
|
},1000 * 60 * 5);
|
67
|
if(!sessionStorage.getItem('name')) {
|
68
|
console.log(`session.name wasn't found --> logging in via repo-service!`);
|
69
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
70
|
userInfo => {
|
71
|
console.log(userInfo.json());
|
72
|
sessionStorage.setItem('name',userInfo.json()['name']);
|
73
|
sessionStorage.setItem('email',userInfo.json()['email']);
|
74
|
sessionStorage.setItem('role',userInfo.json()['role']);
|
75
|
this.isLoggedIn = true;
|
76
|
console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
77
|
},
|
78
|
() => {
|
79
|
sessionStorage.removeItem('name');
|
80
|
sessionStorage.removeItem('email');
|
81
|
sessionStorage.removeItem('role');
|
82
|
deleteCookie('currentUser');
|
83
|
this.isLoggedIn = false;
|
84
|
}
|
85
|
);
|
86
|
} else {
|
87
|
this.isLoggedIn = true;
|
88
|
console.log(`the current user is: ${sessionStorage.getItem('name')}, ${sessionStorage.getItem('email')}, ${sessionStorage.getItem('role')}`);
|
89
|
}
|
90
|
if ( sessionStorage.getItem("state.location") ) {
|
91
|
let state = sessionStorage.getItem("state.location");
|
92
|
sessionStorage.removeItem("state.location");
|
93
|
console.log(`tried to login - returning to state: ${state}`);
|
94
|
if (this.redirectUrl) {
|
95
|
this.router.navigate([this.redirectUrl]);
|
96
|
} else {
|
97
|
this.router.navigate([state]);
|
98
|
}
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
|
103
|
public getIsUserLoggedIn() {
|
104
|
return this.isLoggedIn;
|
105
|
}
|
106
|
|
107
|
public getUserName() {
|
108
|
if (this.isLoggedIn)
|
109
|
return sessionStorage.getItem('name');
|
110
|
else
|
111
|
return '';
|
112
|
}
|
113
|
|
114
|
public getUserEmail() {
|
115
|
if (this.isLoggedIn)
|
116
|
return sessionStorage.getItem('email');
|
117
|
else
|
118
|
return '';
|
119
|
}
|
120
|
|
121
|
public getUserRole() {
|
122
|
if (this.isLoggedIn)
|
123
|
return sessionStorage.getItem('role');
|
124
|
else
|
125
|
return '';
|
126
|
}
|
127
|
|
128
|
}
|