diff --git a/frontend/src/app/feature/transaction-history/transaction-history.component.ts b/frontend/src/app/feature/transaction-history/transaction-history.component.ts index 338ef4e..dabc255 100644 --- a/frontend/src/app/feature/transaction-history/transaction-history.component.ts +++ b/frontend/src/app/feature/transaction-history/transaction-history.component.ts @@ -1,11 +1,4 @@ -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 { Observable } from 'rxjs'; import { AsyncPipe, CurrencyPipe, DatePipe, NgIf } from '@angular/common'; diff --git a/frontend/src/app/service/auth.service.ts b/frontend/src/app/service/auth.service.ts index 47e1fd3..1066008 100644 --- a/frontend/src/app/service/auth.service.ts +++ b/frontend/src/app/service/auth.service.ts @@ -18,24 +18,21 @@ export class AuthService { private authUrl = `${environment.apiUrl}/auth`; private userUrl = `${environment.apiUrl}/users`; - private currentUserSubject: BehaviorSubject; - public currentUser: Observable; + userSubject: BehaviorSubject; constructor( private http: HttpClient, private router: Router ) { - this.currentUserSubject = new BehaviorSubject(this.getUserFromStorage()); - this.currentUser = this.currentUserSubject.asObservable(); + this.userSubject = new BehaviorSubject(this.getUserFromStorage()); - // Check if token exists and load user data if (this.getToken()) { this.loadCurrentUser(); } } public get currentUserValue(): User | null { - return this.currentUserSubject.value; + return this.userSubject.value; } login(loginRequest: LoginRequest): Observable { @@ -54,7 +51,7 @@ export class AuthService { logout(): void { localStorage.removeItem(TOKEN_KEY); localStorage.removeItem(USER_KEY); - this.currentUserSubject.next(null); + this.userSubject.next(null); this.router.navigate(['/']); } @@ -83,7 +80,7 @@ export class AuthService { private setUser(user: User): void { localStorage.setItem(USER_KEY, JSON.stringify(user)); - this.currentUserSubject.next(user); + this.userSubject.next(user); } private getUserFromStorage(): User | null { diff --git a/frontend/src/app/shared/components/navbar/navbar.component.ts b/frontend/src/app/shared/components/navbar/navbar.component.ts index 494b61f..5d44f60 100644 --- a/frontend/src/app/shared/components/navbar/navbar.component.ts +++ b/frontend/src/app/shared/components/navbar/navbar.component.ts @@ -10,7 +10,6 @@ import { RouterModule } from '@angular/router'; import { AuthService } from '@service/auth.service'; import { Subscription } from 'rxjs'; import { AnimatedNumberComponent } from '@blackjack/components/animated-number/animated-number.component'; -import { UserService } from '@service/user.service'; @Component({ selector: 'app-navbar', @@ -24,29 +23,19 @@ export class NavbarComponent implements OnInit, OnDestroy { private authService: AuthService = inject(AuthService); isLoggedIn = this.authService.isLoggedIn(); - private userSubscription: Subscription | undefined; - private authSubscription: Subscription | undefined; + private authSubscription!: Subscription; public balance = signal(0); - private userService = inject(UserService); ngOnInit() { - // Subscribe to auth changes - this.authSubscription = this.authService.currentUser.subscribe((user) => { - this.isLoggedIn = !!user; - this.balance.set(user?.balance ?? 0); - console.log('Updated navbar balance:', user?.balance); + this.authSubscription = this.authService.userSubject.subscribe({ + next: (user) => { + this.balance.set(user?.balance ?? 0); + }, }); - - this.userService.refreshCurrentUser(); } ngOnDestroy() { - if (this.userSubscription) { - this.userSubscription.unsubscribe(); - } - if (this.authSubscription) { - this.authSubscription.unsubscribe(); - } + this.authSubscription.unsubscribe(); } logout() {