userService now persist user data in localStorage
This commit is contained in:
parent
a12b30998d
commit
df20d25a35
2 changed files with 11 additions and 8 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, distinctUntilChanged, tap } from 'rxjs';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import { Recipe, Recipe01 } from '../models/recipe.model';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
|
|
|
|||
|
|
@ -19,17 +19,18 @@ export class UserService {
|
|||
.asObservable()
|
||||
.pipe(distinctUntilChanged());
|
||||
|
||||
public isAuthenticated = this.currentUser.pipe(
|
||||
map((user) => !!user),
|
||||
tap((isAuth) => {
|
||||
console.log('Change auth', isAuth);
|
||||
})
|
||||
);
|
||||
public isAuthenticated = this.currentUser.pipe(map((user) => !!user));
|
||||
|
||||
constructor(
|
||||
private readonly http: HttpClient,
|
||||
private readonly router: Router
|
||||
) {}
|
||||
) {
|
||||
const user = localStorage.getItem('user');
|
||||
|
||||
if (user) {
|
||||
this.currentUserSubject.next(JSON.parse(user));
|
||||
}
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
this.purgeAuth();
|
||||
|
|
@ -59,10 +60,12 @@ export class UserService {
|
|||
}
|
||||
|
||||
setAuth(user: User): void {
|
||||
window.localStorage.setItem('user', JSON.stringify(user));
|
||||
void this.currentUserSubject.next(user);
|
||||
}
|
||||
|
||||
purgeAuth(): void {
|
||||
window.localStorage.removeItem('user');
|
||||
void this.currentUserSubject.next(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue