Projekt

Obecné

Profil

Stáhnout (2.87 KB) Statistiky
| Větev: | Tag: | Revize:
1 37487772 Lukáš Moučka
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
import {User} from '../models/user';
11
import {GlobalVariable} from '../../globals';
12
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
  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
54
  doLogin(loginInput): Observable<boolean> {
55
    return this.loginService.login$Response(loginInput)
56
      .pipe(
57
        tap((userInfo: HttpResponse<UserInfo>) => this.setUserFromResponse(userInfo.body, loginInput.username)),
58
        mapTo(true),
59
        catchError(() => {
60
          return of<boolean>(false);
61
        })
62
      );
63
  }
64
65
  setUserFromResponse(userInfo: UserInfo, username): UserInfo {
66
    userInfo = this.setRights(userInfo, username);
67
    console.log('Setting user from login!');
68
    this.userState.setUser({
69
      userInfo,
70
      isLoggedIn: true,
71
      userName: username
72
    });
73
    this.setSessionStorage(userInfo, username);
74
    return userInfo;
75
  }
76
77
  doLogout() {
78
    this.userState.setUser(null);
79
    this.cookieService.deleteAll();
80
    localStorage.clear();
81
    this.router.navigate(['/login']);
82
  }
83
84
  setSessionStorage(userInfo: UserInfo, username) {
85
    localStorage.setItem(GlobalVariable.USER_NAME, username)
86
    localStorage.setItem(GlobalVariable.RIGHTS, userInfo.rights.toString());
87
  }
88
89
  setRights(userInfo: UserInfo, username: string): UserInfo {
90
    // TODO - todle je pene naho... naprd
91
    if (username === 'kiv') {
92
      userInfo.rights = 1;
93
    } else {
94
      userInfo.rights = 2;
95
    }
96
    return userInfo;
97
  }
98
}