From 9a26d879c9211ee3851ed47f831623d5d4bfdd13 Mon Sep 17 00:00:00 2001 From: Marcel Eisentraut Date: Thu, 5 May 2022 10:00:53 +0200 Subject: [PATCH] fix: cookie page guard should only use NgModal on browser side (#1148) --- src/app/pages/cookies/cookies-page.guard.ts | 49 ++++++++++++--------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/app/pages/cookies/cookies-page.guard.ts b/src/app/pages/cookies/cookies-page.guard.ts index 9875235153..afe96f2567 100644 --- a/src/app/pages/cookies/cookies-page.guard.ts +++ b/src/app/pages/cookies/cookies-page.guard.ts @@ -1,4 +1,5 @@ -import { Injectable } from '@angular/core'; +import { isPlatformBrowser } from '@angular/common'; +import { Inject, Injectable, PLATFORM_ID } from '@angular/core'; import { CanActivate, NavigationEnd, Router } from '@angular/router'; import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { filter, first } from 'rxjs/operators'; @@ -9,32 +10,38 @@ import { CookiesModalComponent } from './cookies-modal/cookies-modal.component'; export class CookiesPageGuard implements CanActivate { private currentDialog: NgbModalRef; - constructor(private modalService: NgbModal, private router: Router) {} + constructor( + @Inject(PLATFORM_ID) private platformId: string, + private modalService: NgbModal, + private router: Router + ) {} async canActivate() { - this.currentDialog = this.modalService.open(CookiesModalComponent, { - centered: true, - size: 'lg', - backdrop: 'static', - }); - - const cookiesModalComponent = this.currentDialog.componentInstance as CookiesModalComponent; + if (isPlatformBrowser(this.platformId)) { + this.currentDialog = this.modalService.open(CookiesModalComponent, { + centered: true, + size: 'lg', + backdrop: 'static', + }); - // dialog closed - cookiesModalComponent.closeModal.pipe(first()).subscribe(() => { - this.currentDialog.dismiss(); - }); + const cookiesModalComponent = this.currentDialog.componentInstance as CookiesModalComponent; - // navigated away with link on dialog - this.router.events - .pipe( - filter(event => event instanceof NavigationEnd), - first() - ) - .subscribe(() => { + // dialog closed + cookiesModalComponent.closeModal.pipe(first()).subscribe(() => { this.currentDialog.dismiss(); }); - return false; + // navigated away with link on dialog + this.router.events + .pipe( + filter(event => event instanceof NavigationEnd), + first() + ) + .subscribe(() => { + this.currentDialog.dismiss(); + }); + return false; + } + return true; } }