Skip to content

Commit

Permalink
fix: prevent navigating twice on user login
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi committed Sep 23, 2020
1 parent 3e55b63 commit 75fdc15
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 33 deletions.
3 changes: 0 additions & 3 deletions src/app/core/guards/login.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 0 additions & 14 deletions src/app/core/store/customer/user/user.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 5 additions & 15 deletions src/app/core/store/customer/user/user.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,6 +16,7 @@ import {
map,
mapTo,
mergeMap,
sample,
switchMap,
switchMapTo,
tap,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' } });
});
}

Expand Down

0 comments on commit 75fdc15

Please sign in to comment.