refactor(auth): replace UserService with AuthService usage
Some checks failed
CI / Get Changed Files (pull_request) Successful in 37s
CI / Docker backend validation (pull_request) Successful in 14s
CI / Docker frontend validation (pull_request) Successful in 51s
CI / eslint (pull_request) Successful in 42s
CI / oxlint (pull_request) Successful in 32s
CI / Checkstyle Main (pull_request) Successful in 1m2s
CI / prettier (pull_request) Failing after 27s
CI / test-build (pull_request) Successful in 59s
Some checks failed
CI / Get Changed Files (pull_request) Successful in 37s
CI / Docker backend validation (pull_request) Successful in 14s
CI / Docker frontend validation (pull_request) Successful in 51s
CI / eslint (pull_request) Successful in 42s
CI / oxlint (pull_request) Successful in 32s
CI / Checkstyle Main (pull_request) Successful in 1m2s
CI / prettier (pull_request) Failing after 27s
CI / test-build (pull_request) Successful in 59s
This commit is contained in:
parent
205bf1e52c
commit
226675de03
6 changed files with 38 additions and 86 deletions
|
@ -14,6 +14,7 @@ import { NavbarComponent } from '@shared/components/navbar/navbar.component';
|
||||||
import { UserService } from '@service/user.service';
|
import { UserService } from '@service/user.service';
|
||||||
import { timer } from 'rxjs';
|
import { timer } from 'rxjs';
|
||||||
import { DebtDialogComponent } from '@shared/components/debt-dialog/debt-dialog.component';
|
import { DebtDialogComponent } from '@shared/components/debt-dialog/debt-dialog.component';
|
||||||
|
import { AuthService } from '@service/auth.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-blackjack',
|
selector: 'app-blackjack',
|
||||||
|
@ -34,6 +35,7 @@ import { DebtDialogComponent } from '@shared/components/debt-dialog/debt-dialog.
|
||||||
export default class BlackjackComponent implements OnInit {
|
export default class BlackjackComponent implements OnInit {
|
||||||
private router = inject(Router);
|
private router = inject(Router);
|
||||||
private userService = inject(UserService);
|
private userService = inject(UserService);
|
||||||
|
private authService = inject(AuthService);
|
||||||
private blackjackService = inject(BlackjackService);
|
private blackjackService = inject(BlackjackService);
|
||||||
|
|
||||||
dealerCards = signal<Card[]>([]);
|
dealerCards = signal<Card[]>([]);
|
||||||
|
@ -51,15 +53,8 @@ export default class BlackjackComponent implements OnInit {
|
||||||
debtAmount = signal(0);
|
debtAmount = signal(0);
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
// Initial user load from server
|
|
||||||
this.userService.getCurrentUser().subscribe((user) => {
|
|
||||||
if (user) {
|
|
||||||
this.balance.set(user.balance);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Subscribe to user updates for real-time balance changes
|
// Subscribe to user updates for real-time balance changes
|
||||||
this.userService.currentUser$.subscribe((user) => {
|
this.authService.userSubject.subscribe((user) => {
|
||||||
if (user) {
|
if (user) {
|
||||||
this.balance.set(user.balance);
|
this.balance.set(user.balance);
|
||||||
}
|
}
|
||||||
|
@ -95,16 +90,11 @@ export default class BlackjackComponent implements OnInit {
|
||||||
|
|
||||||
// Get the latest balance before showing the result dialog
|
// Get the latest balance before showing the result dialog
|
||||||
timer(1000).subscribe(() => {
|
timer(1000).subscribe(() => {
|
||||||
this.userService.getCurrentUser().subscribe((user) => {
|
// Show the result dialog after refreshing user data
|
||||||
if (user) {
|
|
||||||
this.balance.set(user.balance);
|
|
||||||
// Show the result dialog after updating the balance
|
|
||||||
timer(500).subscribe(() => {
|
timer(500).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');
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,15 +176,11 @@ export default class BlackjackComponent implements OnInit {
|
||||||
|
|
||||||
// Wait a bit to ensure the backend has finished processing
|
// Wait a bit to ensure the backend has finished processing
|
||||||
timer(1000).subscribe(() => {
|
timer(1000).subscribe(() => {
|
||||||
this.userService.getCurrentUser().subscribe((user) => {
|
const user = this.authService.currentUserValue;
|
||||||
if (user) {
|
if (user && user.balance < 0) {
|
||||||
this.balance.set(user.balance);
|
|
||||||
if (user.balance < 0) {
|
|
||||||
this.debtAmount.set(Math.abs(user.balance));
|
this.debtAmount.set(Math.abs(user.balance));
|
||||||
this.showDebtDialog.set(true);
|
this.showDebtDialog.set(true);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.isActionInProgress.set(false);
|
this.isActionInProgress.set(false);
|
||||||
|
@ -210,13 +196,6 @@ 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);
|
||||||
|
|
||||||
// Refresh the balance when dialog is closed and update local state
|
|
||||||
this.userService.getCurrentUser().subscribe((user) => {
|
|
||||||
if (user) {
|
|
||||||
this.balance.set(user.balance);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onCloseDebtDialog(): void {
|
onCloseDebtDialog(): void {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import { FormsModule } from '@angular/forms';
|
||||||
import { UserService } from '@service/user.service';
|
import { UserService } from '@service/user.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 { AuthService } from '@service/auth.service';
|
||||||
|
|
||||||
interface SlotResult {
|
interface SlotResult {
|
||||||
status: 'win' | 'lose' | 'blank' | 'start';
|
status: 'win' | 'lose' | 'blank' | 'start';
|
||||||
|
@ -39,6 +40,7 @@ interface SlotResult {
|
||||||
export default class SlotsComponent implements OnInit, OnDestroy {
|
export default class SlotsComponent implements OnInit, OnDestroy {
|
||||||
private httpClient: HttpClient = inject(HttpClient);
|
private httpClient: HttpClient = inject(HttpClient);
|
||||||
private userService = inject(UserService);
|
private userService = inject(UserService);
|
||||||
|
private authService = inject(AuthService);
|
||||||
private userSubscription: Subscription | undefined;
|
private userSubscription: Subscription | undefined;
|
||||||
|
|
||||||
slotInfo = signal<Record<string, number> | null>(null);
|
slotInfo = signal<Record<string, number> | null>(null);
|
||||||
|
@ -61,7 +63,7 @@ export default class SlotsComponent implements OnInit, OnDestroy {
|
||||||
this.slotInfo.set(data);
|
this.slotInfo.set(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.userSubscription = this.userService.getCurrentUser().subscribe((user) => {
|
this.userSubscription = this.authService.userSubject.subscribe((user) => {
|
||||||
this.balance.set(user?.balance ?? 0);
|
this.balance.set(user?.balance ?? 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import { LootBox, Reward } from 'app/model/LootBox';
|
||||||
import { NavbarComponent } from '@shared/components/navbar/navbar.component';
|
import { NavbarComponent } from '@shared/components/navbar/navbar.component';
|
||||||
import { UserService } from '@service/user.service';
|
import { UserService } from '@service/user.service';
|
||||||
import { User } from 'app/model/User';
|
import { User } from 'app/model/User';
|
||||||
|
import { AuthService } from '@service/auth.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-lootbox-opening',
|
selector: 'app-lootbox-opening',
|
||||||
|
@ -30,10 +31,11 @@ export default class LootboxOpeningComponent {
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private lootboxService: LootboxService,
|
private lootboxService: LootboxService,
|
||||||
private userService: UserService,
|
private userService: UserService,
|
||||||
|
private authService: AuthService,
|
||||||
private cdr: ChangeDetectorRef
|
private cdr: ChangeDetectorRef
|
||||||
) {
|
) {
|
||||||
this.loadLootbox();
|
this.loadLootbox();
|
||||||
this.userService.currentUser$.subscribe((user) => {
|
this.authService.userSubject.subscribe((user) => {
|
||||||
this.currentUser = user;
|
this.currentUser = user;
|
||||||
this.cdr.detectChanges();
|
this.cdr.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,8 +5,9 @@ import { LootboxService } from '../services/lootbox.service';
|
||||||
import { LootBox } from 'app/model/LootBox';
|
import { LootBox } from 'app/model/LootBox';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { timeout } from 'rxjs';
|
import { timeout } from 'rxjs';
|
||||||
import { UserService } from '@service/user.service';
|
|
||||||
import { User } from 'app/model/User';
|
import { User } from 'app/model/User';
|
||||||
|
import { AuthService } from '@service/auth.service';
|
||||||
|
import { UserService } from '@service/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-lootbox-selection',
|
selector: 'app-lootbox-selection',
|
||||||
|
@ -90,12 +91,13 @@ export default class LootboxSelectionComponent implements OnInit {
|
||||||
private lootboxService: LootboxService,
|
private lootboxService: LootboxService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private cdr: ChangeDetectorRef,
|
private cdr: ChangeDetectorRef,
|
||||||
|
private authService: AuthService,
|
||||||
private userService: UserService
|
private userService: UserService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.loadLootboxes();
|
this.loadLootboxes();
|
||||||
this.userService.currentUser$.subscribe((user) => {
|
this.authService.userSubject.subscribe((user) => {
|
||||||
this.currentUser = user;
|
this.currentUser = user;
|
||||||
this.cdr.detectChanges();
|
this.cdr.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,51 +1,33 @@
|
||||||
import { inject, Injectable } from '@angular/core';
|
import { inject, Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
|
||||||
import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs';
|
|
||||||
import { User } from '../model/User';
|
|
||||||
import { AuthService } from '@service/auth.service';
|
import { AuthService } from '@service/auth.service';
|
||||||
|
import { User } from '../model/User';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class UserService {
|
export class UserService {
|
||||||
public currentUserSubject = new BehaviorSubject<User | null>(null);
|
|
||||||
public currentUser$ = this.currentUserSubject.asObservable();
|
|
||||||
private http: HttpClient = inject(HttpClient);
|
|
||||||
private authService = inject(AuthService);
|
private authService = inject(AuthService);
|
||||||
|
|
||||||
constructor() {
|
/**
|
||||||
// Initialize with the current user from AuthService if available
|
* Updates the user's balance locally for immediate UI feedback
|
||||||
const currentUser = this.authService.getUser();
|
* This should be called before a server-side balance change is made
|
||||||
if (currentUser) {
|
* The server update will be reflected when AuthService.loadCurrentUser() is called
|
||||||
this.currentUserSubject.next(currentUser);
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe to auth service user updates
|
|
||||||
this.authService.userSubject.subscribe((user) => {
|
|
||||||
this.currentUserSubject.next(user);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public getCurrentUser(): Observable<User | null> {
|
|
||||||
return this.http.get<User | null>('/backend/users/me').pipe(
|
|
||||||
catchError(() => EMPTY),
|
|
||||||
tap((user) => this.currentUserSubject.next(user))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public refreshCurrentUser(): void {
|
|
||||||
this.getCurrentUser().subscribe();
|
|
||||||
this.authService.loadCurrentUser();
|
|
||||||
}
|
|
||||||
|
|
||||||
public updateLocalBalance(amount: number): void {
|
public updateLocalBalance(amount: number): void {
|
||||||
const currentUser = this.currentUserSubject.getValue();
|
const currentUser = this.authService.currentUserValue;
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
const updatedUser = {
|
const updatedUser: User = {
|
||||||
...currentUser,
|
...currentUser,
|
||||||
balance: currentUser.balance + amount,
|
balance: currentUser.balance + amount,
|
||||||
};
|
};
|
||||||
this.currentUserSubject.next(updatedUser);
|
this.authService.userSubject.next(updatedUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refreshes the current user's data from the server
|
||||||
|
*/
|
||||||
|
public refreshCurrentUser(): void {
|
||||||
|
this.authService.loadCurrentUser();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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',
|
||||||
|
@ -22,11 +21,9 @@ import { UserService } from '@service/user.service';
|
||||||
export class NavbarComponent implements OnInit, OnDestroy {
|
export class NavbarComponent implements OnInit, OnDestroy {
|
||||||
isMenuOpen = false;
|
isMenuOpen = false;
|
||||||
private authService: AuthService = inject(AuthService);
|
private authService: AuthService = inject(AuthService);
|
||||||
private userService: UserService = inject(UserService);
|
|
||||||
isLoggedIn = this.authService.isLoggedIn();
|
isLoggedIn = this.authService.isLoggedIn();
|
||||||
|
|
||||||
private authSubscription!: Subscription;
|
private authSubscription!: Subscription;
|
||||||
private userSubscription!: Subscription;
|
|
||||||
public balance = signal(0);
|
public balance = signal(0);
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
@ -35,22 +32,10 @@ export class NavbarComponent implements OnInit, OnDestroy {
|
||||||
this.balance.set(user?.balance ?? 0);
|
this.balance.set(user?.balance ?? 0);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Also subscribe to UserService for real-time balance updates
|
|
||||||
this.userSubscription = this.userService.currentUser$.subscribe({
|
|
||||||
next: (user) => {
|
|
||||||
if (user) {
|
|
||||||
this.balance.set(user.balance);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
this.authSubscription.unsubscribe();
|
this.authSubscription.unsubscribe();
|
||||||
if (this.userSubscription) {
|
|
||||||
this.userSubscription.unsubscribe();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
|
|
Reference in a new issue