Compare commits
15 commits
98668a3fb0
...
4c530edf50
Author | SHA1 | Date | |
---|---|---|---|
4c530edf50 |
|||
e2124e8b8e |
|||
dcf073abfa |
|||
b3c4cb4c83 |
|||
7f90e8cb94 |
|||
064cc78402 |
|||
8d8af1101b |
|||
a9052466e8 |
|||
45551551cc |
|||
2c6d5a755f |
|||
fb4c7f175a |
|||
f7e8fd76a6 |
|||
c8c82af4b8 |
|||
b7279a85ca |
|||
fd079d2460 |
3 changed files with 33 additions and 12 deletions
|
@ -1,4 +1,11 @@
|
||||||
import { ChangeDetectionStrategy, Component, EventEmitter, inject, Input, Output } from '@angular/core';
|
import {
|
||||||
|
ChangeDetectionStrategy,
|
||||||
|
Component,
|
||||||
|
EventEmitter,
|
||||||
|
inject,
|
||||||
|
Input,
|
||||||
|
Output,
|
||||||
|
} from '@angular/core';
|
||||||
import { TransactionService } from '@service/transaction.service';
|
import { TransactionService } from '@service/transaction.service';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { AsyncPipe, CurrencyPipe, DatePipe, NgIf } from '@angular/common';
|
import { AsyncPipe, CurrencyPipe, DatePipe, NgIf } from '@angular/common';
|
||||||
|
|
|
@ -18,21 +18,24 @@ export class AuthService {
|
||||||
private authUrl = `${environment.apiUrl}/auth`;
|
private authUrl = `${environment.apiUrl}/auth`;
|
||||||
private userUrl = `${environment.apiUrl}/users`;
|
private userUrl = `${environment.apiUrl}/users`;
|
||||||
|
|
||||||
userSubject: BehaviorSubject<User | null>;
|
private currentUserSubject: BehaviorSubject<User | null>;
|
||||||
|
public currentUser: Observable<User | null>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private http: HttpClient,
|
private http: HttpClient,
|
||||||
private router: Router
|
private router: Router
|
||||||
) {
|
) {
|
||||||
this.userSubject = new BehaviorSubject<User | null>(this.getUserFromStorage());
|
this.currentUserSubject = new BehaviorSubject<User | null>(this.getUserFromStorage());
|
||||||
|
this.currentUser = this.currentUserSubject.asObservable();
|
||||||
|
|
||||||
|
// Check if token exists and load user data
|
||||||
if (this.getToken()) {
|
if (this.getToken()) {
|
||||||
this.loadCurrentUser();
|
this.loadCurrentUser();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get currentUserValue(): User | null {
|
public get currentUserValue(): User | null {
|
||||||
return this.userSubject.value;
|
return this.currentUserSubject.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(loginRequest: LoginRequest): Observable<AuthResponse> {
|
login(loginRequest: LoginRequest): Observable<AuthResponse> {
|
||||||
|
@ -51,7 +54,7 @@ export class AuthService {
|
||||||
logout(): void {
|
logout(): void {
|
||||||
localStorage.removeItem(TOKEN_KEY);
|
localStorage.removeItem(TOKEN_KEY);
|
||||||
localStorage.removeItem(USER_KEY);
|
localStorage.removeItem(USER_KEY);
|
||||||
this.userSubject.next(null);
|
this.currentUserSubject.next(null);
|
||||||
this.router.navigate(['/']);
|
this.router.navigate(['/']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +83,7 @@ export class AuthService {
|
||||||
|
|
||||||
private setUser(user: User): void {
|
private setUser(user: User): void {
|
||||||
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
||||||
this.userSubject.next(user);
|
this.currentUserSubject.next(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getUserFromStorage(): User | null {
|
private getUserFromStorage(): User | null {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { RouterModule } from '@angular/router';
|
||||||
import { AuthService } from '@service/auth.service';
|
import { AuthService } from '@service/auth.service';
|
||||||
import { Subscription } from 'rxjs';
|
import { Subscription } from 'rxjs';
|
||||||
import { AnimatedNumberComponent } from '@blackjack/components/animated-number/animated-number.component';
|
import { AnimatedNumberComponent } from '@blackjack/components/animated-number/animated-number.component';
|
||||||
|
import { UserService } from '@service/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-navbar',
|
selector: 'app-navbar',
|
||||||
|
@ -23,20 +24,30 @@ export class NavbarComponent implements OnInit, OnDestroy {
|
||||||
private authService: AuthService = inject(AuthService);
|
private authService: AuthService = inject(AuthService);
|
||||||
isLoggedIn = this.authService.isLoggedIn();
|
isLoggedIn = this.authService.isLoggedIn();
|
||||||
|
|
||||||
private authSubscription!: Subscription;
|
private userSubscription: Subscription | undefined;
|
||||||
|
private authSubscription: Subscription | undefined;
|
||||||
public balance = signal(0);
|
public balance = signal(0);
|
||||||
|
private userService = inject(UserService);
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.authSubscription = this.authService.userSubject.subscribe({
|
// Subscribe to auth changes
|
||||||
next: (user) => {
|
this.authSubscription = this.authService.currentUser.subscribe((user) => {
|
||||||
|
this.isLoggedIn = !!user;
|
||||||
this.balance.set(user?.balance ?? 0);
|
this.balance.set(user?.balance ?? 0);
|
||||||
},
|
console.log('Updated navbar balance:', user?.balance);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.userService.refreshCurrentUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
|
if (this.userSubscription) {
|
||||||
|
this.userSubscription.unsubscribe();
|
||||||
|
}
|
||||||
|
if (this.authSubscription) {
|
||||||
this.authSubscription.unsubscribe();
|
this.authSubscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
this.authService.logout();
|
this.authService.logout();
|
||||||
|
|
Reference in a new issue