From 9906d5fec02da8996a654795b2fad2a6c4f14e9e Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 3 Mar 2022 11:10:35 +0100 Subject: [PATCH] 88082: Revert navigateAction on redirectOn4xx --- src/app/core/shared/operators.ts | 14 +++----------- src/app/register-page/registration.guard.spec.ts | 4 +--- src/app/register-page/registration.guard.ts | 8 ++------ 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/src/app/core/shared/operators.ts b/src/app/core/shared/operators.ts index c53ca20b1d..3be04447ab 100644 --- a/src/app/core/shared/operators.ts +++ b/src/app/core/shared/operators.ts @@ -197,27 +197,19 @@ export const getAllSucceededRemoteListPayload = () => * * @param router The router used to navigate to a new page * @param authService Service to check if the user is authenticated - * @param navigateAction Optional action to take with the Promise returned by navigateByUrl */ -// tslint:disable-next-line:no-empty -export const redirectOn4xx = (router: Router, authService: AuthService, navigateAction?: (nav: boolean) => void) => +export const redirectOn4xx = (router: Router, authService: AuthService) => (source: Observable>): Observable> => source.pipe( withLatestFrom(authService.isAuthenticated()), filter(([rd, isAuthenticated]: [RemoteData, boolean]) => { if (rd.hasFailed) { if (rd.statusCode === 404 || rd.statusCode === 422) { - const promise = router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true }); - if (hasValue(navigateAction)) { - promise.then(navigateAction); - } + router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true }); return false; } else if (rd.statusCode === 403 || rd.statusCode === 401) { if (isAuthenticated) { - const promise = router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true }); - if (hasValue(navigateAction)) { - promise.then(navigateAction); - } + router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true }); return false; } else { authService.setRedirectUrl(router.url); diff --git a/src/app/register-page/registration.guard.spec.ts b/src/app/register-page/registration.guard.spec.ts index 0dfa57c7bf..89eaff7a02 100644 --- a/src/app/register-page/registration.guard.spec.ts +++ b/src/app/register-page/registration.guard.spec.ts @@ -17,7 +17,6 @@ describe('RegistrationGuard', () => { let epersonRegistrationService: EpersonRegistrationService; let router: Router; let authService: AuthService; - let location: Location; let registration: Registration; let registrationRD: RemoteData; @@ -61,9 +60,8 @@ describe('RegistrationGuard', () => { isAuthenticated: observableOf(false), setRedirectUrl: {}, }); - location = jasmine.createSpyObj('location', ['replaceState']); - guard = new RegistrationGuard(epersonRegistrationService, router, authService, location); + guard = new RegistrationGuard(epersonRegistrationService, router, authService); }); describe('canActivate', () => { diff --git a/src/app/register-page/registration.guard.ts b/src/app/register-page/registration.guard.ts index 2be3f3fe53..36bbfa4252 100644 --- a/src/app/register-page/registration.guard.ts +++ b/src/app/register-page/registration.guard.ts @@ -18,8 +18,7 @@ import { Location } from '@angular/common'; export class RegistrationGuard implements CanActivate { constructor(private epersonRegistrationService: EpersonRegistrationService, private router: Router, - private authService: AuthService, - private location: Location) { + private authService: AuthService) { } /** @@ -33,10 +32,7 @@ export class RegistrationGuard implements CanActivate { const token = route.params.token; return this.epersonRegistrationService.searchByToken(token).pipe( getFirstCompletedRemoteData(), - // The replaceState call is an action taken by the Promise returned by navigateByUrl within redirectOn4xx - // It ensures the user stays on the correct URL when redirected to a 4xx page - // See this related issue's comment: https://github.com/angular/angular/issues/16981#issuecomment-549330207 - redirectOn4xx(this.router, this.authService, () => this.location.replaceState(state.url)), + redirectOn4xx(this.router, this.authService), map((rd) => { route.data = { ...route.data, registration: rd }; return rd.hasSucceeded;