From 75fdc15f6d9f4076a80cf1ff0a95ca600845a6f0 Mon Sep 17 00:00:00 2001 From: Danilo Hoffmann Date: Thu, 10 Sep 2020 11:27:04 +0200 Subject: [PATCH] fix: prevent navigating twice on user login --- src/app/core/guards/login.guard.ts | 3 --- .../store/customer/user/user.effects.spec.ts | 14 ------------- .../core/store/customer/user/user.effects.ts | 20 +++++-------------- .../update-password.component.ts | 2 +- 4 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/app/core/guards/login.guard.ts b/src/app/core/guards/login.guard.ts index 0c94df36f6..96c517d855 100644 --- a/src/app/core/guards/login.guard.ts +++ b/src/app/core/guards/login.guard.ts @@ -34,8 +34,6 @@ export class LoginGuard implements CanActivate { return true; } - const returnUrl = route.queryParams.returnUrl || '/home'; - this.currentDialog = this.modalService.open(LoginModalComponent, { centered: true, size: 'sm' }); const loginModalComponent = this.currentDialog.componentInstance as LoginModalComponent; @@ -59,7 +57,6 @@ export class LoginGuard implements CanActivate { // login successful this.store.pipe(select(getUserAuthorized), whenTruthy(), first()).subscribe(() => { this.currentDialog.dismiss(); - this.router.navigateByUrl(returnUrl); }); return false; diff --git a/src/app/core/store/customer/user/user.effects.spec.ts b/src/app/core/store/customer/user/user.effects.spec.ts index 6f922d28a9..1cae023fa1 100644 --- a/src/app/core/store/customer/user/user.effects.spec.ts +++ b/src/app/core/store/customer/user/user.effects.spec.ts @@ -231,20 +231,6 @@ describe('User Effects', () => { expect(location.path()).toEqual('/foobar'); })); - it('should navigate to /account after LoginUserSuccess when no returnUrl is set and user is at login', fakeAsync(() => { - router.navigate(['/login']); - tick(500); - expect(location.path()).toEqual('/login'); - - store$.dispatch(loginUserSuccess(loginResponseData)); - - effects.redirectAfterLogin$.subscribe(noop, fail, noop); - - tick(500); - - expect(location.path()).toEqual('/account'); - })); - it('should not navigate after LoginUserSuccess when user is logged in and somewhere else', fakeAsync(() => { router.navigate(['/home']); tick(500); diff --git a/src/app/core/store/customer/user/user.effects.ts b/src/app/core/store/customer/user/user.effects.ts index ab011124fa..1c5a0fb9c1 100644 --- a/src/app/core/store/customer/user/user.effects.ts +++ b/src/app/core/store/customer/user/user.effects.ts @@ -3,7 +3,7 @@ import { Router } from '@angular/router'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { routerNavigatedAction } from '@ngrx/router-store'; import { Store, select } from '@ngrx/store'; -import { EMPTY, merge, race, timer } from 'rxjs'; +import { EMPTY, race, timer } from 'rxjs'; import { catchError, concatMap, @@ -16,6 +16,7 @@ import { map, mapTo, mergeMap, + sample, switchMap, switchMapTo, tap, @@ -27,7 +28,7 @@ import { PaymentService } from 'ish-core/services/payment/payment.service'; import { PersonalizationService } from 'ish-core/services/personalization/personalization.service'; import { UserService } from 'ish-core/services/user/user.service'; import { displaySuccessMessage } from 'ish-core/store/core/messages'; -import { ofUrl, selectQueryParam, selectUrl } from 'ish-core/store/core/router'; +import { selectQueryParam, selectUrl } from 'ish-core/store/core/router'; import { mapErrorToAction, mapToPayload, mapToPayloadProperty, whenTruthy } from 'ish-core/utils/operators'; import { @@ -129,20 +130,9 @@ export class UserEffects { */ redirectAfterLogin$ = createEffect( () => - merge( - this.actions$.pipe( - ofType(loginUserSuccess), - switchMapTo(this.store$.pipe(select(selectQueryParam('returnUrl')), first())), - whenTruthy() - ), - this.store$.pipe( - ofUrl(/^\/login.*/), - select(selectQueryParam('returnUrl')), - map(returnUrl => returnUrl || '/account'), - switchMap(returnUrl => this.store$.pipe(select(getLoggedInUser), whenTruthy(), mapTo(returnUrl))) - ) - ).pipe( + this.store$.pipe(select(selectQueryParam('returnUrl'))).pipe( whenTruthy(), + sample(this.actions$.pipe(ofType(loginUserSuccess))), tap(navigateTo => this.router.navigateByUrl(navigateTo)) ), { dispatch: false } diff --git a/src/app/pages/forgot-password/update-password/update-password.component.ts b/src/app/pages/forgot-password/update-password/update-password.component.ts index a7a48ba230..635d2b6b9e 100644 --- a/src/app/pages/forgot-password/update-password/update-password.component.ts +++ b/src/app/pages/forgot-password/update-password/update-password.component.ts @@ -41,7 +41,7 @@ export class UpdatePasswordComponent implements OnInit, OnDestroy { }); this.accountFacade.passwordReminderSuccess$.pipe(whenTruthy(), takeUntil(this.destroy$)).subscribe(() => { - this.router.navigate(['/login'], { queryParams: { forcePageView: true } }); + this.router.navigate(['/login'], { queryParams: { forcePageView: true, returnUrl: '/account' } }); }); }