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