fix: fix login/register
Some checks failed
CI / Get Changed Files (pull_request) Successful in 8s
CI / Checkstyle Main (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Successful in 10s
CI / Docker frontend validation (pull_request) Successful in 36s
CI / oxlint (pull_request) Successful in 25s
CI / prettier (pull_request) Failing after 26s
CI / eslint (pull_request) Successful in 32s
CI / test-build (pull_request) Successful in 41s
Some checks failed
CI / Get Changed Files (pull_request) Successful in 8s
CI / Checkstyle Main (pull_request) Has been skipped
CI / Docker backend validation (pull_request) Successful in 10s
CI / Docker frontend validation (pull_request) Successful in 36s
CI / oxlint (pull_request) Successful in 25s
CI / prettier (pull_request) Failing after 26s
CI / eslint (pull_request) Successful in 32s
CI / test-build (pull_request) Successful in 41s
This commit is contained in:
parent
62e7e0ec65
commit
ad77c76d14
2 changed files with 14 additions and 33 deletions
|
@ -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<User | null>;
|
||||
userSubject: BehaviorSubject<User | null>;
|
||||
public currentUser: Observable<User | null>;
|
||||
|
||||
constructor(
|
||||
private http: HttpClient,
|
||||
private router: Router
|
||||
) {
|
||||
this.currentUserSubject = new BehaviorSubject<User | null>(this.getUserFromStorage());
|
||||
this.currentUser = this.currentUserSubject.asObservable();
|
||||
this.userSubject = new BehaviorSubject<User | null>(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<AuthResponse> {
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Reference in a new issue