feat(auth): add user refresh functionality in services
Some checks failed
CI / Get Changed Files (pull_request) Successful in 9s
CI / Docker backend validation (pull_request) Successful in 10s
CI / Docker frontend validation (pull_request) Failing after 36s
CI / oxlint (pull_request) Failing after 27s
CI / eslint (pull_request) Failing after 33s
CI / Checkstyle Main (pull_request) Successful in 46s
CI / prettier (pull_request) Failing after 23s
CI / test-build (pull_request) Failing after 31s

This commit is contained in:
Constantin Simonis 2025-05-07 15:35:33 +02:00
commit 84250969aa
No known key found for this signature in database
GPG key ID: 3878FF77C24AF4D2
3 changed files with 24 additions and 18 deletions

View file

@ -83,7 +83,7 @@ export default class BlackjackComponent implements OnInit {
if (isGameOver) { if (isGameOver) {
console.log('Game is over, state:', game.state); console.log('Game is over, state:', game.state);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
timer(1500).subscribe(() => { timer(1500).subscribe(() => {
this.showGameResult.set(true); this.showGameResult.set(true);
console.log('Game result dialog shown after delay'); console.log('Game result dialog shown after delay');
@ -97,7 +97,7 @@ export default class BlackjackComponent implements OnInit {
this.blackjackService.startGame(bet).subscribe({ this.blackjackService.startGame(bet).subscribe({
next: (game) => { next: (game) => {
this.updateGameState(game); this.updateGameState(game);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
this.isActionInProgress.set(false); this.isActionInProgress.set(false);
}, },
error: (error) => { error: (error) => {
@ -116,7 +116,7 @@ export default class BlackjackComponent implements OnInit {
next: (game) => { next: (game) => {
this.updateGameState(game); this.updateGameState(game);
if (game.state !== 'IN_PROGRESS') { if (game.state !== 'IN_PROGRESS') {
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
} }
this.isActionInProgress.set(false); this.isActionInProgress.set(false);
}, },
@ -141,7 +141,7 @@ export default class BlackjackComponent implements OnInit {
this.blackjackService.stand(this.currentGameId()!).subscribe({ this.blackjackService.stand(this.currentGameId()!).subscribe({
next: (game) => { next: (game) => {
this.updateGameState(game); this.updateGameState(game);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
this.isActionInProgress.set(false); this.isActionInProgress.set(false);
}, },
error: (error) => { error: (error) => {
@ -184,7 +184,7 @@ export default class BlackjackComponent implements OnInit {
onCloseGameResult(): void { onCloseGameResult(): void {
console.log('Closing game result dialog'); console.log('Closing game result dialog');
this.showGameResult.set(false); this.showGameResult.set(false);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
} }
onCloseDebtDialog(): void { onCloseDebtDialog(): void {
@ -195,11 +195,11 @@ export default class BlackjackComponent implements OnInit {
if (error instanceof HttpErrorResponse) { if (error instanceof HttpErrorResponse) {
if (error.status === 400 && error.error?.error === 'Invalid state') { if (error.status === 400 && error.error?.error === 'Invalid state') {
this.gameInProgress.set(false); this.gameInProgress.set(false);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
} else if (error.status === 500) { } else if (error.status === 500) {
console.log('Server error occurred. The game may have been updated in another session.'); console.log('Server error occurred. The game may have been updated in another session.');
this.gameInProgress.set(false); this.gameInProgress.set(false);
// this.userService.refreshCurrentUser(); this.userService.refreshCurrentUser();
if (this.currentGameId()) { if (this.currentGameId()) {
this.refreshGameState(this.currentGameId()!); this.refreshGameState(this.currentGameId()!);
} }

View file

@ -66,6 +66,17 @@ export class AuthService {
return localStorage.getItem(TOKEN_KEY); return localStorage.getItem(TOKEN_KEY);
} }
public loadCurrentUser(): void {
this.http.get<User>(`${this.userUrl}/me`).subscribe({
next: (user) => {
this.setUser(user);
},
error: () => {
this.logout();
},
});
}
private setToken(token: string): void { private setToken(token: string): void {
localStorage.setItem(TOKEN_KEY, token); localStorage.setItem(TOKEN_KEY, token);
} }
@ -80,17 +91,6 @@ export class AuthService {
return user ? JSON.parse(user) : null; return user ? JSON.parse(user) : null;
} }
private loadCurrentUser(): void {
this.http.get<User>(`${this.userUrl}/me`).subscribe({
next: (user) => {
this.setUser(user);
},
error: () => {
this.logout();
},
});
}
getUser(): User | null { getUser(): User | null {
return this.currentUserValue; return this.currentUserValue;
} }

View file

@ -3,6 +3,7 @@ import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs'; import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs';
import { User } from '../model/User'; import { User } from '../model/User';
import { environment } from '@environments/environment'; import { environment } from '@environments/environment';
import { AuthService } from '@service/auth.service';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -12,6 +13,7 @@ export class UserService {
private currentUserSubject = new BehaviorSubject<User | null>(null); private currentUserSubject = new BehaviorSubject<User | null>(null);
private http: HttpClient = inject(HttpClient); private http: HttpClient = inject(HttpClient);
private authService: AuthService = inject(AuthService);
public getCurrentUser(): Observable<User | null> { public getCurrentUser(): Observable<User | null> {
@ -35,4 +37,8 @@ export class UserService {
this.currentUserSubject.next(updatedUser); this.currentUserSubject.next(updatedUser);
} }
} }
public refreshCurrentUser(): void {
this.authService.loadCurrentUser();
}
} }