From f2ee2b467da338fac07e8ec1917f0f7b132b36bb Mon Sep 17 00:00:00 2001 From: Michel ten Voorde Date: Tue, 30 Sep 2025 22:59:13 +0200 Subject: [PATCH] Fixed authguard --- src/app/app.config.ts | 13 +++-- src/app/authentication/authguard.ts | 9 ++-- src/app/authentication/user.service.ts | 51 ++++++++++++------- .../tournament-manage.component.html | 2 +- .../tournament-players.component.ts | 12 +---- src/app/service/header.service.ts | 1 - 6 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 611212c..0879a89 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,4 +1,4 @@ -import {ApplicationConfig, provideZoneChangeDetection} from '@angular/core'; +import {ApplicationConfig, inject, provideAppInitializer, provideZoneChangeDetection} from '@angular/core'; import {provideRouter} from '@angular/router'; import {routes} from './app.routes'; @@ -10,15 +10,21 @@ import {MAT_SNACK_BAR_DEFAULT_OPTIONS} from "@angular/material/snack-bar"; import {AuthGuard} from "./authentication/authguard"; import {TokenInterceptor} from "./authentication/tokenInterceptor"; import {ErrorInterceptor} from "./authentication/errorInterceptor"; -import {provideEnvironmentNgxMask, provideNgxMask} from "ngx-mask"; +import {provideNgxMask} from "ngx-mask"; +import {UserService} from './authentication/user.service'; +import {provideAnimations} from "@angular/platform-browser/animations"; export const appConfig: ApplicationConfig = { providers: [ + provideAppInitializer(() => { + const userService = inject(UserService); + userService.initializeUser(); + }), provideZoneChangeDetection({eventCoalescing: true}), provideRouter(routes), provideClientHydration(), provideHttpClient(withFetch(), withInterceptorsFromDi()), - provideAnimationsAsync(), + provideAnimations(), provideMomentDateAdapter(undefined, {useUtc: false}), { provide: MAT_SNACK_BAR_DEFAULT_OPTIONS, useValue: { duration: 2500}}, AuthGuard, @@ -27,3 +33,4 @@ export const appConfig: ApplicationConfig = { provideNgxMask(), ] }; + diff --git a/src/app/authentication/authguard.ts b/src/app/authentication/authguard.ts index f0b35b2..d04bd5a 100644 --- a/src/app/authentication/authguard.ts +++ b/src/app/authentication/authguard.ts @@ -1,6 +1,7 @@ import {Injectable} from "@angular/core"; import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from "@angular/router"; import {UserService} from "./user.service"; +import {delay, map, Observable, of} from "rxjs"; @Injectable() export class AuthGuard implements CanActivate { @@ -10,14 +11,12 @@ export class AuthGuard implements CanActivate { private userService: UserService ) {} - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (this.userService.getUser()) { return true; } - - this.router.navigate(['/auth/login'], { - queryParams: { returnUrl: state.url }, - }); + this.router.navigate(['/auth/login'], { queryParams: { returnUrl: state.url } }); return false; } + } diff --git a/src/app/authentication/user.service.ts b/src/app/authentication/user.service.ts index 5cd4dc8..cbc9248 100644 --- a/src/app/authentication/user.service.ts +++ b/src/app/authentication/user.service.ts @@ -1,16 +1,17 @@ -import {EventEmitter, Injectable, Output} from "@angular/core"; -import {SsrCookieService} from "ngx-cookie-service-ssr"; -import {Router} from "@angular/router"; -import {User} from "./user"; -import {BehaviorSubject} from "rxjs"; +import { Injectable } from "@angular/core"; +import { SsrCookieService } from "ngx-cookie-service-ssr"; +import { Router } from "@angular/router"; +import { User } from "./user"; +import { BehaviorSubject } from "rxjs"; @Injectable({ providedIn: 'root' }) export class UserService { - user?: User; + private user?: User; + private userEmitter = new BehaviorSubject(''); + private initialized = false; - private userEmitter = new BehaviorSubject(''); currentUser = this.userEmitter.asObservable(); constructor( @@ -18,30 +19,44 @@ export class UserService { private router: Router, ) {} - public getUser(): User | undefined { - const user = this.cookieService.get('swissuser'); + initializeUser() { + if (this.initialized) return; - if (user) { - this.user = JSON.parse(user); - this.userEmitter.next(JSON.parse(user).username); - } else { + try { + const userCookie = this.cookieService.get('swissuser'); + if (userCookie && userCookie.trim() !== '') { + this.user = JSON.parse(userCookie); + if (this.user?.username) { + this.userEmitter.next(this.user.username); + } else { + this.userEmitter.next(''); + } + } else { + this.user = undefined; + this.userEmitter.next(''); + } + } catch (error) { + console.error('Error parsing user cookie:', error); this.user = undefined; + this.userEmitter.next(''); } + this.initialized = true; + } + + public getUser(): User | undefined { + this.initializeUser(); return this.user; } public isLoggedIn(): boolean { - const user = this.cookieService.get('swissuser'); - - return !(user == undefined || false || user == ""); + this.initializeUser(); + return !!this.user; } public setUser(user: User) { this.cookieService.set('swissuser', JSON.stringify(user)); - this.user = user; - this.userEmitter.next(this.user.username); } diff --git a/src/app/components/tournament-manage/tournament-manage.component.html b/src/app/components/tournament-manage/tournament-manage.component.html index 6213d4c..0c723a6 100644 --- a/src/app/components/tournament-manage/tournament-manage.component.html +++ b/src/app/components/tournament-manage/tournament-manage.component.html @@ -124,7 +124,7 @@ @if (this.activeMatches().length > 0) { -
Actieve wedstrijden
+
@for (activeMatch of this.activeMatches(); track activeMatch.match.id) { diff --git a/src/app/components/tournament-players/tournament-players.component.ts b/src/app/components/tournament-players/tournament-players.component.ts index 3945b38..2f74d64 100644 --- a/src/app/components/tournament-players/tournament-players.component.ts +++ b/src/app/components/tournament-players/tournament-players.component.ts @@ -34,25 +34,15 @@ import {MatTab, MatTabGroup, MatTabLabel} from "@angular/material/tabs"; standalone: true, styleUrl: './tournament-players.component.scss' }) -export class TournamentPlayersComponent implements OnInit { +export class TournamentPlayersComponent { @Input() tournament: Tournament; constructor( private tournamentService: TournamentService, private _snackBar: MatSnackBar, - private route: ActivatedRoute, - private router: Router, ) {} - ngOnInit() { - console.log('Tournament received from parent:', this.tournament); - // const id = this.route.snapshot.paramMap.get('id'); - // this.tournamentService.getById(Number(id)).subscribe(data => { - // this.tournament = data; - // }); - } - playerPaid($event: MatSlideToggleChange, playerId: number) { this.tournamentService.playerPaid(this.tournament.id, playerId, $event.checked).subscribe(() => { this._snackBar.open('Opgeslagen.'); diff --git a/src/app/service/header.service.ts b/src/app/service/header.service.ts index f3c5fcf..1ad5bd5 100644 --- a/src/app/service/header.service.ts +++ b/src/app/service/header.service.ts @@ -1,4 +1,3 @@ -// header.service.ts import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs';