From ad77c76d14426cc2960bde430d00e5b5f1b2c2c9 Mon Sep 17 00:00:00 2001 From: Constantin Simonis Date: Wed, 7 May 2025 17:23:51 +0200 Subject: [PATCH] fix: fix login/register --- frontend/src/app/service/auth.service.ts | 15 ++++----- .../components/navbar/navbar.component.ts | 32 ++++--------------- 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/frontend/src/app/service/auth.service.ts b/frontend/src/app/service/auth.service.ts index 47e1fd3..16ab864 100644 --- a/frontend/src/app/service/auth.service.ts +++ b/frontend/src/app/service/auth.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { BehaviorSubject, Observable, tap } from 'rxjs'; +import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs'; import { Router } from '@angular/router'; import { LoginRequest } from '../model/auth/LoginRequest'; import { RegisterRequest } from '../model/auth/RegisterRequest'; @@ -18,24 +18,23 @@ export class AuthService { private authUrl = `${environment.apiUrl}/auth`; private userUrl = `${environment.apiUrl}/users`; - private currentUserSubject: BehaviorSubject; + userSubject: BehaviorSubject; public currentUser: Observable; constructor( private http: HttpClient, private router: Router ) { - this.currentUserSubject = new BehaviorSubject(this.getUserFromStorage()); - this.currentUser = this.currentUserSubject.asObservable(); + this.userSubject = new BehaviorSubject(this.getUserFromStorage()); + this.currentUser = this.userSubject.asObservable().pipe(catchError(() => EMPTY)); - // 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 +53,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 +82,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..24e2aee 100644 --- a/frontend/src/app/shared/components/navbar/navbar.component.ts +++ b/frontend/src/app/shared/components/navbar/navbar.component.ts @@ -1,16 +1,8 @@ -import { - ChangeDetectionStrategy, - Component, - inject, - OnDestroy, - OnInit, - signal, -} from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit, signal } from '@angular/core'; 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 +16,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() {