Revize 2c5da396
Přidáno uživatelem Jakub Hlaváč před téměř 4 roky(ů)
src/app/auth/services/auth.service.ts | ||
---|---|---|
1 | 1 |
import {Injectable} from '@angular/core'; |
2 | 2 |
import {HttpClient, HttpResponse} from '@angular/common/http'; |
3 |
import {Observable, of, pipe} from 'rxjs';
|
|
3 |
import {Observable, of} from 'rxjs'; |
|
4 | 4 |
import {catchError, mapTo, tap} from 'rxjs/operators'; |
5 | 5 |
import {Router} from '@angular/router'; |
6 | 6 |
import {CookieService} from 'ngx-cookie-service'; |
7 | 7 |
import {UserState} from '../states/user.state'; |
8 | 8 |
import {LoginService} from '../../shared/api/endpoints/services/login.service'; |
9 | 9 |
import {UserInfo} from '../../shared/api/endpoints/models/user-info'; |
10 |
import {Group} from '../../shared/api/endpoints/models/group'; |
|
10 |
import {User} from '../models/user'; |
|
11 |
import {GlobalVariable} from '../../globals'; |
|
11 | 12 |
|
12 | 13 |
@Injectable({ |
13 | 14 |
providedIn: 'root' |
... | ... | |
23 | 24 |
) { |
24 | 25 |
} |
25 | 26 |
|
27 |
setFromCookie() { |
|
28 |
const user: User = { |
|
29 |
userInfo: { |
|
30 |
rights: JSON.parse(localStorage.getItem(GlobalVariable.RIGHTS)), |
|
31 |
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 |
userName: localStorage.getItem(GlobalVariable.USER_NAME), |
|
36 |
isLoggedIn: true |
|
37 |
} |
|
38 |
this.userState.setUser(user); |
|
39 |
} |
|
40 |
|
|
41 |
getUser() { |
|
42 |
console.log('Auth Service', this.userState.getUser()); |
|
43 |
if (!this.userState.getUser() && this.cookieService.get(GlobalVariable.SESSION_ID)) { |
|
44 |
console.log('Session in cookie!'); |
|
45 |
this.setFromCookie(); |
|
46 |
} |
|
47 |
return this.userState.getUser(); |
|
48 |
} |
|
49 |
|
|
50 |
getUserState(): Observable<User> { |
|
51 |
return this.userState.getUser$(); |
|
52 |
} |
|
53 |
|
|
26 | 54 |
doLogin(loginInput): Observable<boolean> { |
27 | 55 |
return this.loginService.login$Response(loginInput) |
28 | 56 |
.pipe( |
29 |
tap((userInfo: HttpResponse<UserInfo>) => this.setUserFromResponse(userInfo.body, loginInput)), |
|
57 |
tap((userInfo: HttpResponse<UserInfo>) => this.setUserFromResponse(userInfo.body, loginInput.username)),
|
|
30 | 58 |
mapTo(true), |
31 | 59 |
catchError(() => { |
32 | 60 |
return of<boolean>(false); |
... | ... | |
34 | 62 |
); |
35 | 63 |
} |
36 | 64 |
|
37 |
getUserState(): Observable<UserInfo> { |
|
38 |
return this.userState.getUser$(); |
|
39 |
} |
|
40 |
|
|
41 |
getIsLoggedIn(): boolean { |
|
42 |
return this.userState.getLoggedIn(); |
|
43 |
} |
|
44 |
|
|
45 |
setUserFromResponse(userInfo: UserInfo, loginInput): UserInfo { |
|
46 |
this.userState.setUser(userInfo); |
|
47 |
this.userState.setLoggedIn(true); |
|
48 |
sessionStorage.setItem('userName', loginInput.username) |
|
49 |
sessionStorage.setItem('sessionid', userInfo.sessionid) |
|
50 |
sessionStorage.setItem('language', userInfo.language) |
|
51 |
sessionStorage.setItem('audio', String(userInfo.audio)) |
|
52 |
this.cookieService.set('userName', loginInput.username) |
|
65 |
setUserFromResponse(userInfo: UserInfo, username): UserInfo { |
|
66 |
console.log('Setting user from login!'); |
|
67 |
this.userState.setUser({ |
|
68 |
userInfo, |
|
69 |
isLoggedIn: true, |
|
70 |
userName: username |
|
71 |
}); |
|
72 |
this.setSessionStorage(userInfo, username); |
|
53 | 73 |
return userInfo; |
54 | 74 |
} |
55 | 75 |
|
56 | 76 |
doLogout() { |
77 |
this.userState.setUser(null); |
|
57 | 78 |
this.cookieService.deleteAll(); |
79 |
localStorage.clear(); |
|
80 |
this.router.navigate(['/login']); |
|
58 | 81 |
} |
59 | 82 |
|
60 |
getUserRoles(): Group[] { |
|
61 |
return this.userState.getUserGroups(); |
|
83 |
setSessionStorage(userInfo: UserInfo, username) { |
|
84 |
localStorage.setItem(GlobalVariable.USER_NAME, username) |
|
85 |
// TODO - get rights from response |
|
86 |
// localStorage.setItem('rights', userInfo.rights.toString()); |
|
87 |
localStorage.setItem(GlobalVariable.RIGHTS, '1'); |
|
62 | 88 |
} |
63 | 89 |
} |
Také k dispozici: Unified diff
Re #8469 - Improvment security - implementace
+ usage of localStorage for fields
+ loading properties form cookies to state