diff --git a/frontend/src/app/feature/game/blackjack/blackjack.component.ts b/frontend/src/app/feature/game/blackjack/blackjack.component.ts index e261260..2763b55 100644 --- a/frontend/src/app/feature/game/blackjack/blackjack.component.ts +++ b/frontend/src/app/feature/game/blackjack/blackjack.component.ts @@ -83,7 +83,7 @@ export default class BlackjackComponent implements OnInit { if (isGameOver) { console.log('Game is over, state:', game.state); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); timer(1500).subscribe(() => { this.showGameResult.set(true); console.log('Game result dialog shown after delay'); @@ -97,7 +97,7 @@ export default class BlackjackComponent implements OnInit { this.blackjackService.startGame(bet).subscribe({ next: (game) => { this.updateGameState(game); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); this.isActionInProgress.set(false); }, error: (error) => { @@ -116,7 +116,7 @@ export default class BlackjackComponent implements OnInit { next: (game) => { this.updateGameState(game); if (game.state !== 'IN_PROGRESS') { - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); } this.isActionInProgress.set(false); }, @@ -141,7 +141,7 @@ export default class BlackjackComponent implements OnInit { this.blackjackService.stand(this.currentGameId()!).subscribe({ next: (game) => { this.updateGameState(game); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); this.isActionInProgress.set(false); }, error: (error) => { @@ -184,7 +184,7 @@ export default class BlackjackComponent implements OnInit { onCloseGameResult(): void { console.log('Closing game result dialog'); this.showGameResult.set(false); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); } onCloseDebtDialog(): void { @@ -195,11 +195,11 @@ export default class BlackjackComponent implements OnInit { if (error instanceof HttpErrorResponse) { if (error.status === 400 && error.error?.error === 'Invalid state') { this.gameInProgress.set(false); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); } else if (error.status === 500) { console.log('Server error occurred. The game may have been updated in another session.'); this.gameInProgress.set(false); - // this.userService.refreshCurrentUser(); + this.userService.refreshCurrentUser(); if (this.currentGameId()) { this.refreshGameState(this.currentGameId()!); } diff --git a/frontend/src/app/service/auth.service.ts b/frontend/src/app/service/auth.service.ts index 1a118cd..47e1fd3 100644 --- a/frontend/src/app/service/auth.service.ts +++ b/frontend/src/app/service/auth.service.ts @@ -66,6 +66,17 @@ export class AuthService { return localStorage.getItem(TOKEN_KEY); } + public loadCurrentUser(): void { + this.http.get(`${this.userUrl}/me`).subscribe({ + next: (user) => { + this.setUser(user); + }, + error: () => { + this.logout(); + }, + }); + } + private setToken(token: string): void { localStorage.setItem(TOKEN_KEY, token); } @@ -80,17 +91,6 @@ export class AuthService { return user ? JSON.parse(user) : null; } - private loadCurrentUser(): void { - this.http.get(`${this.userUrl}/me`).subscribe({ - next: (user) => { - this.setUser(user); - }, - error: () => { - this.logout(); - }, - }); - } - getUser(): User | null { return this.currentUserValue; } diff --git a/frontend/src/app/service/user.service.ts b/frontend/src/app/service/user.service.ts index 5391945..aa2a687 100644 --- a/frontend/src/app/service/user.service.ts +++ b/frontend/src/app/service/user.service.ts @@ -3,6 +3,7 @@ import { HttpClient } from '@angular/common/http'; import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs'; import { User } from '../model/User'; import { environment } from '@environments/environment'; +import { AuthService } from '@service/auth.service'; @Injectable({ providedIn: 'root', @@ -12,6 +13,7 @@ export class UserService { private currentUserSubject = new BehaviorSubject(null); private http: HttpClient = inject(HttpClient); + private authService: AuthService = inject(AuthService); public getCurrentUser(): Observable { @@ -35,4 +37,8 @@ export class UserService { this.currentUserSubject.next(updatedUser); } } + + public refreshCurrentUser(): void { + this.authService.loadCurrentUser(); + } }