1
|
import {Injectable} from "@angular/core";
|
2
|
import { ActivatedRoute, Router } from '@angular/router';
|
3
|
import { apiUrl, loginUrl } from '../domain/tempAPI';
|
4
|
import { deleteCookie, getCookie } from '../domain/utils';
|
5
|
import { Http } from '@angular/http';
|
6
|
import { User } from '../domain/typeScriptClasses';
|
7
|
import {CookieService} from "angular2-cookie/core";
|
8
|
|
9
|
@Injectable()
|
10
|
export class AuthenticationService {
|
11
|
|
12
|
constructor(private route: ActivatedRoute,
|
13
|
private router: Router,
|
14
|
private http: Http,
|
15
|
private _cookie: CookieService) {}
|
16
|
|
17
|
private loginUrl : string = loginUrl;
|
18
|
private apiUrl : string = apiUrl;
|
19
|
|
20
|
// store the URL so we can redirect after logging in
|
21
|
public redirectUrl: string;
|
22
|
|
23
|
private _storage: Storage = sessionStorage;
|
24
|
|
25
|
|
26
|
isLoggedIn: boolean = false;
|
27
|
userEmail: string;
|
28
|
userFullName: string;
|
29
|
userRole: string;
|
30
|
|
31
|
public loginWithState() {
|
32
|
console.log(`logging in with state. Current url is: ${this.router.url}`);
|
33
|
sessionStorage.setItem("state.location",this.router.url);
|
34
|
window.location.href = this.loginUrl;
|
35
|
}
|
36
|
|
37
|
login(user: User){
|
38
|
localStorage.setItem('user', JSON.stringify(user));
|
39
|
this.isLoggedIn = true;
|
40
|
this.userEmail = localStorage.getItem('email');
|
41
|
this.userFullName = localStorage.getItem('name');
|
42
|
this.userRole = localStorage.getItem('role');
|
43
|
}
|
44
|
|
45
|
logout(){
|
46
|
deleteCookie('currentUser');
|
47
|
sessionStorage.removeItem('name');
|
48
|
sessionStorage.removeItem('email');
|
49
|
sessionStorage.removeItem('role');
|
50
|
this.router.navigate(['/home']);
|
51
|
}
|
52
|
|
53
|
public tryLogin() {
|
54
|
|
55
|
if(getCookie('currentUser')) {
|
56
|
console.log(`I got the cookie!`);
|
57
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
58
|
userInfo => {console.log("User is still logged in")},
|
59
|
() => {sessionStorage.removeItem('name');sessionStorage.removeItem('email');deleteCookie('name');sessionStorage.removeItem('role');},
|
60
|
() => {
|
61
|
if(!sessionStorage.getItem('name')) {
|
62
|
console.log(`session.name wasn't found --> logging in via repo-service!`);
|
63
|
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe(
|
64
|
userInfo => {
|
65
|
console.log(userInfo.json());
|
66
|
sessionStorage.setItem('name',userInfo.json()['name']);
|
67
|
sessionStorage.setItem('email',userInfo.json()['email']);
|
68
|
sessionStorage.setItem('role',userInfo.json()['role']);
|
69
|
},
|
70
|
() => {
|
71
|
sessionStorage.removeItem('name');
|
72
|
sessionStorage.removeItem('email');
|
73
|
sessionStorage.removeItem('role');
|
74
|
deleteCookie('currentUser');
|
75
|
}, () => {
|
76
|
this.isLoggedIn = true;
|
77
|
}
|
78
|
);
|
79
|
}
|
80
|
if(sessionStorage.getItem("state.location")) {
|
81
|
let state = sessionStorage.getItem("state.location");
|
82
|
sessionStorage.removeItem("state.location");
|
83
|
this.router.navigateByUrl(state);
|
84
|
}
|
85
|
}
|
86
|
);
|
87
|
}
|
88
|
}
|
89
|
|
90
|
public getIsUserLoggedIn() {
|
91
|
return this.isLoggedIn;
|
92
|
}
|
93
|
|
94
|
public getUserName() {
|
95
|
if (this.isLoggedIn)
|
96
|
return sessionStorage.getItem('name');
|
97
|
}
|
98
|
|
99
|
public getUserEmail() {
|
100
|
if (this.isLoggedIn)
|
101
|
return sessionStorage.getItem('email');
|
102
|
}
|
103
|
|
104
|
public getUserRole() {
|
105
|
if (this.isLoggedIn)
|
106
|
return sessionStorage.getItem('role');
|
107
|
}
|
108
|
|
109
|
}
|