1 |
f8b9a3a2
|
hlavja
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpClient, HttpResponse} from '@angular/common/http';
|
3 |
2c5da396
|
hlavja
|
import {Observable, of} from 'rxjs';
|
4 |
f8b9a3a2
|
hlavja
|
import {catchError, mapTo, tap} from 'rxjs/operators';
|
5 |
|
|
import {Router} from '@angular/router';
|
6 |
|
|
import {CookieService} from 'ngx-cookie-service';
|
7 |
|
|
import {UserState} from '../states/user.state';
|
8 |
|
|
import {LoginService} from '../../shared/api/endpoints/services/login.service';
|
9 |
2c5da396
|
hlavja
|
import {User} from '../models/user';
|
10 |
|
|
import {GlobalVariable} from '../../globals';
|
11 |
87c1bdd1
|
hlavja
|
import {UserCookie} from '../../shared/api/endpoints/models/user-cookie';
|
12 |
f8b9a3a2
|
hlavja
|
|
13 |
|
|
@Injectable({
|
14 |
|
|
providedIn: 'root'
|
15 |
|
|
})
|
16 |
|
|
export class AuthService {
|
17 |
|
|
|
18 |
|
|
constructor(
|
19 |
|
|
private http: HttpClient,
|
20 |
|
|
private userState: UserState,
|
21 |
|
|
private router: Router,
|
22 |
|
|
private cookieService: CookieService,
|
23 |
|
|
private loginService: LoginService
|
24 |
|
|
) {
|
25 |
|
|
}
|
26 |
|
|
|
27 |
2c5da396
|
hlavja
|
setFromCookie() {
|
28 |
|
|
const user: User = {
|
29 |
87c1bdd1
|
hlavja
|
userCookie: {
|
30 |
|
|
rightsID: JSON.parse(localStorage.getItem(GlobalVariable.RIGHTS)),
|
31 |
2c5da396
|
hlavja
|
language: this.cookieService.get(GlobalVariable.LANGUAGE),
|
32 |
|
|
audio: JSON.parse(this.cookieService.get(GlobalVariable.AUDIO)),
|
33 |
|
|
sessionid: this.cookieService.get(GlobalVariable.SESSION_ID)
|
34 |
|
|
},
|
35 |
|
|
isLoggedIn: true
|
36 |
|
|
}
|
37 |
|
|
this.userState.setUser(user);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
getUser() {
|
41 |
87c1bdd1
|
hlavja
|
console.log('Auth Service');
|
42 |
2c5da396
|
hlavja
|
if (!this.userState.getUser() && this.cookieService.get(GlobalVariable.SESSION_ID)) {
|
43 |
|
|
console.log('Session in cookie!');
|
44 |
|
|
this.setFromCookie();
|
45 |
|
|
}
|
46 |
|
|
return this.userState.getUser();
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
getUserState(): Observable<User> {
|
50 |
|
|
return this.userState.getUser$();
|
51 |
|
|
}
|
52 |
|
|
|
53 |
f8b9a3a2
|
hlavja
|
doLogin(loginInput): Observable<boolean> {
|
54 |
|
|
return this.loginService.login$Response(loginInput)
|
55 |
|
|
.pipe(
|
56 |
87c1bdd1
|
hlavja
|
tap((userCookie: HttpResponse<UserCookie>) => this.setUserFromResponse(userCookie.body, loginInput.username)),
|
57 |
f8b9a3a2
|
hlavja
|
mapTo(true),
|
58 |
|
|
catchError(() => {
|
59 |
|
|
return of<boolean>(false);
|
60 |
|
|
})
|
61 |
|
|
);
|
62 |
|
|
}
|
63 |
|
|
|
64 |
87c1bdd1
|
hlavja
|
setUserFromResponse(userCookie: UserCookie, username): UserCookie {
|
65 |
2c5da396
|
hlavja
|
console.log('Setting user from login!');
|
66 |
|
|
this.userState.setUser({
|
67 |
87c1bdd1
|
hlavja
|
userCookie,
|
68 |
|
|
isLoggedIn: true
|
69 |
2c5da396
|
hlavja
|
});
|
70 |
87c1bdd1
|
hlavja
|
this.setSessionStorage(userCookie, username);
|
71 |
|
|
return userCookie;
|
72 |
f8b9a3a2
|
hlavja
|
}
|
73 |
|
|
|
74 |
|
|
doLogout() {
|
75 |
2c5da396
|
hlavja
|
this.userState.setUser(null);
|
76 |
f8b9a3a2
|
hlavja
|
this.cookieService.deleteAll();
|
77 |
2c5da396
|
hlavja
|
localStorage.clear();
|
78 |
|
|
this.router.navigate(['/login']);
|
79 |
b887ecc3
|
hlavja
|
this.cookieService.deleteAll();
|
80 |
f8b9a3a2
|
hlavja
|
}
|
81 |
2ddd52c9
|
hlavja
|
|
82 |
87c1bdd1
|
hlavja
|
setSessionStorage(userCookie: UserCookie, username) {
|
83 |
2c5da396
|
hlavja
|
localStorage.setItem(GlobalVariable.USER_NAME, username)
|
84 |
87c1bdd1
|
hlavja
|
localStorage.setItem(GlobalVariable.RIGHTS, userCookie.rightsID.toString());
|
85 |
cd2a65f3
|
hlavja
|
this.cookieService.set('user', username);
|
86 |
2ddd52c9
|
hlavja
|
}
|
87 |
b5525aef
|
hlavja
|
|
88 |
|
|
redirectToDashboard() {
|
89 |
|
|
this.router.navigate(['/dashboard']);
|
90 |
|
|
}
|
91 |
f8b9a3a2
|
hlavja
|
}
|