first commit
This commit is contained in:
64
src/app/score/pending-score.guard.ts
Normal file
64
src/app/score/pending-score.guard.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user