added login and register methods to AccountService
This commit is contained in:
@@ -1,17 +1,38 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
|
import { map } from 'rxjs/operators';
|
||||||
|
|
||||||
|
import { environment } from '../../environments/environment';
|
||||||
|
import { User } from './user';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class AccountService {
|
export class AccountService {
|
||||||
|
private userSubject: BehaviorSubject<User>;
|
||||||
|
public user: Observable<User>;
|
||||||
|
|
||||||
userName: string = null;
|
constructor(private httpClient: HttpClient) {
|
||||||
|
this.userSubject = new BehaviorSubject<User>(JSON.parse(localStorage.getItem('user')));
|
||||||
|
this.user = this.userSubject.asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
constructor(private httpClient: HttpClient) { }
|
public get userValue() {
|
||||||
|
return this.userSubject.value;
|
||||||
|
}
|
||||||
|
|
||||||
login(username, password) {
|
login(username, password) {
|
||||||
return this.httpClient.post<User>
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user