diff --git a/src/app/pages/error/valid-code.guard.ts b/src/app/pages/error/valid-code.guard.ts index 70d3197..c460f73 100644 --- a/src/app/pages/error/valid-code.guard.ts +++ b/src/app/pages/error/valid-code.guard.ts @@ -20,7 +20,7 @@ export class ValidCodeGuard implements CanActivate { */ public canActivate(route: ActivatedRouteSnapshot): boolean | UrlTree { // show empty error page if no code is present - if (route?.queryParamMap?.has('code') ?? false) { + if (!(route?.queryParamMap?.has('code') ?? false)) { return true } @@ -28,17 +28,12 @@ export class ValidCodeGuard implements CanActivate { if (!/^\d{3}$/.test(route?.queryParamMap?.get('code') ?? '')) { console.info(`No valid error code found, redirecting to ${StatusCodes.NOT_FOUND}.`) - // prepare retry url - const retry = new URL(this.document.location?.href ?? '') - retry.searchParams.delete('code') - retry.searchParams.delete('retry') - // redirect to 404 if not valid - return this.router.createUrlTree([this.document.location?.pathname ?? '/'], { + return this.router.createUrlTree([this.document?.location?.pathname ?? '/'], { queryParams: { code: StatusCodes.NOT_FOUND, home: route?.queryParams.home, - retry: retry.toString(), + retry: this.buildRetryUrl(), }, }) } @@ -46,4 +41,22 @@ export class ValidCodeGuard implements CanActivate { // show error page for code parameter return true } + + /** + * Tries to build a retry URL from the current location href. + * + * @returns Returns the retry URL string or null. + */ + private buildRetryUrl(): string | null { + if (this.document?.location?.href?.length === 0) { + return null + } + + // remove code and retry parameters to prevent infinite loop + const retry = new URL(this.document?.location?.href ?? '') + retry.searchParams.delete('code') + retry.searchParams.delete('retry') + + return retry.toString() + } }