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 (state.includes('landing')) {
|
95
|
this.router.navigate([this.redirectUrl]);
|
96
|
} else {
|
97
|
this.router.navigate([state]);
|
98
|
}
|
99
|
}
|
100
|
} else {
|
101
|
this.isLoggedIn = false;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public getIsUserLoggedIn() {
|
106
|
return this.isLoggedIn;
|
107
|
}
|
108
|
|
109
|
public getUserName() {
|
110
|
if (this.isLoggedIn)
|
111
|
return sessionStorage.getItem('name');
|
112
|
else
|
113
|
return '';
|
114
|
}
|
115
|
|
116
|
public getUserEmail() {
|
117
|
if (this.isLoggedIn)
|
118
|
return sessionStorage.getItem('email');
|
119
|
else
|
120
|
return '';
|
121
|
}
|
122
|
|
123
|
public getUserRole() {
|
124
|
if (this.isLoggedIn)
|
125
|
return sessionStorage.getItem('role');
|
126
|
else
|
127
|
return '';
|
128
|
}
|
129
|
|
130
|
}
|