Files
iWuzzler/src/app/score/pending-score.guard.ts
2025-07-22 22:10:30 +02:00

65 lines
1.9 KiB
TypeScript

import { Injectable } from '@angular/core';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router,
} from '@angular/router';
import { Observable, of } from 'rxjs';
import { take, switchMap } from 'rxjs/operators';
import { AuthService } from '../auth/authService/auth.service';
import { PendingActionsService } from './pending-actions.service';
import { NotifierService } from '../shared/notifierService/notifier.service';
@Injectable({
providedIn: 'root',
})
export class PendingScoreGuard implements CanActivate {
constructor(
private authService: AuthService,
private pendingActionsService: PendingActionsService,
private router: Router,
private notifierService: NotifierService
) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> {
return this.authService.currentUser$.pipe(
take(1),
switchMap((user) => {
if (user) {
// User is logged in, allow access to the route
return of(true);
} else {
// User is not logged in, store the score and redirect to login
const score = route.queryParams['score'];
const uid = route.queryParams['uid'];
if (score && uid) {
const uniqueID = this.pendingActionsService.saveTempQRCodeData({
score,
uid,
});
this.pendingActionsService.setTempQRId(uniqueID);
this.router.navigate(['/login'], {
queryParams: {
score: score,
uid: uid,
},
});
this.notifierService.showNotification(
'Bitte registriere oder logge dich ein, um ein Spiel zu deinem Konto hinzuzufügen.',
'OK'
);
return of(false);
} else {
this.router.navigate(['/login']);
return of(false);
}
}
})
);
}
}