1 |
f8b9a3a2
|
hlavja
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpClient, HttpResponse} from '@angular/common/http';
|
3 |
|
|
import {Observable, of} from 'rxjs';
|
4 |
|
|
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 |
|
|
import {UserInfo} from '../../shared/api/endpoints/models/user-info';
|
10 |
2ddd52c9
|
hlavja
|
import {Group} from '../../shared/api/endpoints/models/group';
|
11 |
f8b9a3a2
|
hlavja
|
|
12 |
|
|
@Injectable({
|
13 |
|
|
providedIn: 'root'
|
14 |
|
|
})
|
15 |
|
|
export class AuthService {
|
16 |
|
|
|
17 |
|
|
constructor(
|
18 |
|
|
private http: HttpClient,
|
19 |
|
|
private userState: UserState,
|
20 |
|
|
private router: Router,
|
21 |
|
|
private cookieService: CookieService,
|
22 |
|
|
private loginService: LoginService
|
23 |
|
|
) {
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
doLogin(loginInput): Observable<boolean> {
|
27 |
|
|
return this.loginService.login$Response(loginInput)
|
28 |
|
|
.pipe(
|
29 |
|
|
tap((userInfo: HttpResponse<UserInfo>) => this.setUserFromResponse(userInfo.body)),
|
30 |
|
|
mapTo(true),
|
31 |
|
|
catchError(() => {
|
32 |
|
|
return of<boolean>(false);
|
33 |
|
|
})
|
34 |
|
|
);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
getUserState(): Observable<UserInfo> {
|
38 |
|
|
return this.userState.getUser$();
|
39 |
|
|
}
|
40 |
|
|
|
41 |
2ddd52c9
|
hlavja
|
getIsLoggedIn(): boolean {
|
42 |
|
|
return this.userState.getLoggedIn();
|
43 |
|
|
}
|
44 |
|
|
|
45 |
f8b9a3a2
|
hlavja
|
setUserFromResponse(userInfo: UserInfo): UserInfo {
|
46 |
|
|
this.userState.setUser(userInfo);
|
47 |
|
|
this.userState.setLoggedIn(true);
|
48 |
|
|
return userInfo;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
doLogout() {
|
52 |
|
|
this.cookieService.deleteAll();
|
53 |
|
|
}
|
54 |
2ddd52c9
|
hlavja
|
|
55 |
|
|
getUserRoles(): Group[] {
|
56 |
|
|
return this.userState.getUserGroups();
|
57 |
|
|
}
|
58 |
f8b9a3a2
|
hlavja
|
}
|