Compare commits
20 commits
4c530edf50
...
98668a3fb0
Author | SHA1 | Date | |
---|---|---|---|
98668a3fb0 |
|||
a622dbc7f4 |
|||
4a312dda35 |
|||
88c2f49b03 |
|||
db6bf4f199 |
|||
ad77c76d14 |
|||
62e7e0ec65 |
|||
5a0d7b2313 |
|||
91e546226d |
|||
84250969aa |
|||
851cfe1bc8 |
|||
1111c91407 |
|||
a178386355 |
|||
0cf2ea3438 |
|||
8ba6d4e4d9 |
|||
b1e173f44b |
|||
29a53ea781 |
|||
3b95725d88 |
|||
51540c930b |
|||
35d8fbaea0 |
3 changed files with 12 additions and 33 deletions
|
@ -1,11 +1,4 @@
|
||||||
import {
|
import { ChangeDetectionStrategy, Component, EventEmitter, inject, Input, Output } from '@angular/core';
|
||||||
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,24 +18,21 @@ export class AuthService {
|
||||||
private authUrl = `${environment.apiUrl}/auth`;
|
private authUrl = `${environment.apiUrl}/auth`;
|
||||||
private userUrl = `${environment.apiUrl}/users`;
|
private userUrl = `${environment.apiUrl}/users`;
|
||||||
|
|
||||||
private currentUserSubject: BehaviorSubject<User | null>;
|
userSubject: BehaviorSubject<User | null>;
|
||||||
public currentUser: Observable<User | null>;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private http: HttpClient,
|
private http: HttpClient,
|
||||||
private router: Router
|
private router: Router
|
||||||
) {
|
) {
|
||||||
this.currentUserSubject = new BehaviorSubject<User | null>(this.getUserFromStorage());
|
this.userSubject = 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.currentUserSubject.value;
|
return this.userSubject.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
login(loginRequest: LoginRequest): Observable<AuthResponse> {
|
login(loginRequest: LoginRequest): Observable<AuthResponse> {
|
||||||
|
@ -54,7 +51,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.currentUserSubject.next(null);
|
this.userSubject.next(null);
|
||||||
this.router.navigate(['/']);
|
this.router.navigate(['/']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +80,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.currentUserSubject.next(user);
|
this.userSubject.next(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getUserFromStorage(): User | null {
|
private getUserFromStorage(): User | null {
|
||||||
|
|
|
@ -10,7 +10,6 @@ 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',
|
||||||
|
@ -24,30 +23,20 @@ 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 userSubscription: Subscription | undefined;
|
private authSubscription!: Subscription;
|
||||||
private authSubscription: Subscription | undefined;
|
|
||||||
public balance = signal(0);
|
public balance = signal(0);
|
||||||
private userService = inject(UserService);
|
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
// Subscribe to auth changes
|
this.authSubscription = this.authService.userSubject.subscribe({
|
||||||
this.authSubscription = this.authService.currentUser.subscribe((user) => {
|
next: (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