feat(auth): add user refresh functionality in services
This commit is contained in:
parent
b3c4cb4c83
commit
dcf073abfa
3 changed files with 24 additions and 18 deletions
|
@ -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()!);
|
||||
}
|
||||
|
|
|
@ -66,6 +66,17 @@ export class AuthService {
|
|||
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 {
|
||||
localStorage.setItem(TOKEN_KEY, token);
|
||||
}
|
||||
|
@ -80,17 +91,6 @@ export class AuthService {
|
|||
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 {
|
||||
return this.currentUserValue;
|
||||
}
|
||||
|
|
|
@ -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<User | null>(null);
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
private authService: AuthService = inject(AuthService);
|
||||
|
||||
|
||||
public getCurrentUser(): Observable<User | null> {
|
||||
|
@ -35,4 +37,8 @@ export class UserService {
|
|||
this.currentUserSubject.next(updatedUser);
|
||||
}
|
||||
}
|
||||
|
||||
public refreshCurrentUser(): void {
|
||||
this.authService.loadCurrentUser();
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue