|
|
|
@@ -1,24 +1,32 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import { Router } from '@angular/router'; |
|
|
|
import { HttpClient } from '@angular/common/http'; |
|
|
|
import { BehaviorSubject, Observable } from 'rxjs'; |
|
|
|
import { BehaviorSubject, Observable, switchMap } from 'rxjs'; |
|
|
|
import { map } from 'rxjs/operators'; |
|
|
|
|
|
|
|
import {AuthService, UserJsonld} from "@app/core/api/v1"; |
|
|
|
|
|
|
|
import { AuthService, UserJsonld, UserService } from "@app/core/api/v1"; |
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' }) |
|
|
|
export class AccountService { |
|
|
|
|
|
|
|
private userSubject: BehaviorSubject<UserJsonld | null>; |
|
|
|
public user: Observable<UserJsonld | null>; |
|
|
|
|
|
|
|
constructor( |
|
|
|
private router: Router, |
|
|
|
private http: HttpClient, |
|
|
|
private authService: AuthService |
|
|
|
private authService: AuthService, |
|
|
|
private userService: UserService, |
|
|
|
) { |
|
|
|
this.userSubject = new BehaviorSubject(JSON.parse(localStorage.getItem('user')!)); |
|
|
|
let userData = null; |
|
|
|
const storedUser = localStorage.getItem('user'); |
|
|
|
if (storedUser) { |
|
|
|
try { |
|
|
|
userData = JSON.parse(storedUser); |
|
|
|
} catch (error) { |
|
|
|
localStorage.removeItem('user'); |
|
|
|
} |
|
|
|
} |
|
|
|
this.userSubject = new BehaviorSubject(userData); |
|
|
|
this.user = this.userSubject.asObservable(); |
|
|
|
} |
|
|
|
|
|
|
|
@@ -28,16 +36,25 @@ export class AccountService { |
|
|
|
|
|
|
|
login(email: string, password: string) { |
|
|
|
return this.authService.postCredentialsItem({ email, password }) |
|
|
|
.pipe(map(response => { |
|
|
|
localStorage.setItem('user', JSON.stringify(response.user)); |
|
|
|
this.userSubject.next(response.user || null); |
|
|
|
return response.user; |
|
|
|
})); |
|
|
|
.pipe( |
|
|
|
switchMap(response => { |
|
|
|
localStorage.setItem('token', response.token!); |
|
|
|
this.userService.configuration.credentials = { 'JWT': () => response.token }; |
|
|
|
return this.userService.usersIdGet(response.dbId!.toString()).pipe( |
|
|
|
map(user => { |
|
|
|
const userWithToken = { ...user, token: response.token }; |
|
|
|
localStorage.setItem('user', JSON.stringify(userWithToken)); |
|
|
|
this.userSubject.next(userWithToken); |
|
|
|
return userWithToken; |
|
|
|
}) |
|
|
|
); |
|
|
|
}) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
logout() { |
|
|
|
// remove user from local storage and set current user to null |
|
|
|
localStorage.removeItem('user'); |
|
|
|
localStorage.removeItem('token'); |
|
|
|
this.userSubject.next(null); |
|
|
|
this.router.navigate(['/account/login']); |
|
|
|
} |
|
|
|
@@ -46,17 +63,11 @@ export class AccountService { |
|
|
|
return this.userValue !== null; |
|
|
|
} |
|
|
|
|
|
|
|
isUserAdmin(): boolean { |
|
|
|
if (this.userValue && this.userValue?.roles) { |
|
|
|
return this.userValue?.roles?.includes('ROLE_ADMIN'); |
|
|
|
} |
|
|
|
return false; |
|
|
|
isUserAdmin(): boolean { |
|
|
|
return this.userValue?.roles?.includes('ROLE_ADMIN') ?? false; |
|
|
|
} |
|
|
|
|
|
|
|
userHasRole(role: string): boolean { |
|
|
|
if (this.userValue && this.userValue?.roles) { |
|
|
|
return this.userValue?.roles?.includes(role); |
|
|
|
} |
|
|
|
return false; |
|
|
|
return this.userValue?.roles?.includes(role) ?? false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |