Revision 51194
Added by Myrto Koukouli over 6 years ago
authentication.service.ts | ||
---|---|---|
1 | 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'; |
|
2 | 7 |
|
3 | 8 |
@Injectable() |
4 | 9 |
export class AuthenticationService { |
10 |
|
|
11 |
constructor(private route: ActivatedRoute, private router: Router, private http: Http) {} |
|
12 |
|
|
13 |
private loginUrl : string = loginUrl; |
|
14 |
private apiUrl : string = apiUrl; |
|
15 |
|
|
16 |
// store the URL so we can redirect after logging in |
|
17 |
redirectUrl: string; |
|
18 |
|
|
19 |
private _storage: Storage = sessionStorage; |
|
20 |
|
|
21 |
|
|
5 | 22 |
isLoggedIn: boolean = false; |
6 |
username: string; |
|
7 | 23 |
userEmail: string; |
8 | 24 |
userFullName: string; |
25 |
userRole: string; |
|
9 | 26 |
|
10 |
login(){ |
|
27 |
public loginWithState() { |
|
28 |
console.log(this.route); |
|
29 |
sessionStorage.setItem("state.location",this.router.url); |
|
30 |
window.location.href =this.loginUrl; |
|
31 |
} |
|
32 |
|
|
33 |
public getLoggedInUser() : string { |
|
34 |
console.log(sessionStorage.getItem('name')); |
|
35 |
return sessionStorage.getItem('name'); |
|
36 |
} |
|
37 |
|
|
38 |
login(user: User){ |
|
39 |
localStorage.setItem('user', JSON.stringify(user)); |
|
11 | 40 |
this.isLoggedIn = true; |
12 |
this.username = 'antleb';
|
|
13 |
this.userEmail = 'ant.lebesis@gmail.com';
|
|
14 |
this.userFullName = 'Antonis Lempesis';
|
|
41 |
this.userEmail = localStorage.getItem('email');
|
|
42 |
this.userFullName = localStorage.getItem('name');
|
|
43 |
this.userRole = localStorage.getItem('role');
|
|
15 | 44 |
} |
16 | 45 |
|
17 | 46 |
logout(){ |
47 |
deleteCookie('currentUser'); |
|
48 |
/* sessionStorage.removeItem('user');*/ |
|
49 |
window.location.href = `/`; |
|
18 | 50 |
this.isLoggedIn = false; |
19 |
this.username = '';
|
|
51 |
this.userRole = '';
|
|
20 | 52 |
this.userEmail = ''; |
21 | 53 |
this.userFullName = ''; |
22 | 54 |
} |
23 | 55 |
|
24 |
register(){ |
|
25 |
|
|
56 |
public tryLogin() { |
|
57 |
if(getCookie('currentUser')) { |
|
58 |
setInterval(() =>{ |
|
59 |
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe( |
|
60 |
userInfo => {console.log("User is still logged in")}, |
|
61 |
() => {sessionStorage.removeItem('name');sessionStorage.removeItem('email');deleteCookie('name');sessionStorage.removeItem('role');} |
|
62 |
); |
|
63 |
},1000 * 60 * 5); |
|
64 |
if(!sessionStorage.getItem('name')) { |
|
65 |
this.http.get(this.apiUrl + '/user/login',{ withCredentials: true }).subscribe( |
|
66 |
userInfo => { |
|
67 |
console.log(userInfo.json()); |
|
68 |
sessionStorage.setItem('name',userInfo.json()['name']); |
|
69 |
sessionStorage.setItem('email',userInfo.json()['email']); |
|
70 |
sessionStorage.setItem('role',userInfo.json()['role']); |
|
71 |
}, |
|
72 |
() => {sessionStorage.removeItem('name');sessionStorage.removeItem('email');deleteCookie('name');sessionStorage.removeItem('role');} |
|
73 |
); |
|
74 |
} |
|
75 |
if(sessionStorage.getItem("state.location")) { |
|
76 |
let state = sessionStorage.getItem("state.location"); |
|
77 |
sessionStorage.removeItem("state.location"); |
|
78 |
this.router.navigateByUrl(state); |
|
79 |
} |
|
80 |
} |
|
26 | 81 |
} |
27 | 82 |
|
28 |
getUserEmail() { |
|
83 |
|
|
84 |
|
|
85 |
getUserEmail() { |
|
29 | 86 |
return this.userEmail; |
30 | 87 |
} |
31 | 88 |
} |
Also available in: Unified diff
almost finished login