2020-07-21 08:45:40 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
2020-07-21 15:19:49 +02:00
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
import { environment } from '../../environments/environment';
|
|
|
|
|
import { User } from './user';
|
2020-07-21 08:45:40 +02:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class AccountService {
|
2020-07-21 15:19:49 +02:00
|
|
|
private userSubject: BehaviorSubject<User>;
|
|
|
|
|
public user: Observable<User>;
|
2020-07-21 08:45:40 +02:00
|
|
|
|
2020-07-21 15:19:49 +02:00
|
|
|
constructor(private httpClient: HttpClient) {
|
|
|
|
|
this.userSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('user')));
|
|
|
|
|
this.user = this.userSubject.asObservable();
|
|
|
|
|
}
|
2020-07-21 08:45:40 +02:00
|
|
|
|
2020-07-21 15:19:49 +02:00
|
|
|
public get userValue() {
|
|
|
|
|
return this.userSubject.value;
|
|
|
|
|
}
|
2020-07-21 08:45:40 +02:00
|
|
|
|
|
|
|
|
login(username, password) {
|
2020-07-21 15:19:49 +02:00
|
|
|
return this.httpClient.post<User>(environment.apiUrl + '/fake_login', { username, password })
|
|
|
|
|
.pipe((map(user => {
|
|
|
|
|
localStorage.setItem('user', JSON.stringify(user));
|
|
|
|
|
this.userSubject.next(user);
|
|
|
|
|
return user;
|
|
|
|
|
})))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
register(user) {
|
|
|
|
|
return this.httpClient.post<User>(environment.apiUrl + '/fake_registration', user);
|
2020-07-21 08:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|