diff --git a/src/app/core/end-user-agreement/end-user-agreement.guard.spec.ts b/src/app/core/end-user-agreement/end-user-agreement.guard.spec.ts index 589b227b5a..a7f3f32a6b 100644 --- a/src/app/core/end-user-agreement/end-user-agreement.guard.spec.ts +++ b/src/app/core/end-user-agreement/end-user-agreement.guard.spec.ts @@ -2,23 +2,26 @@ import { EndUserAgreementGuard } from './end-user-agreement.guard'; import { EndUserAgreementService } from './end-user-agreement.service'; import { Router, UrlTree } from '@angular/router'; import { of as observableOf } from 'rxjs'; +import { AuthService } from '../auth/auth.service'; describe('EndUserAgreementGuard', () => { let guard: EndUserAgreementGuard; let endUserAgreementService: EndUserAgreementService; + let authService: AuthService; let router: Router; beforeEach(() => { endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', { hasCurrentUserAcceptedAgreement: observableOf(true) }); + authService = jasmine.createSpyObj('authService', ['setRedirectUrl']); router = jasmine.createSpyObj('router', { navigateByUrl: {}, parseUrl: new UrlTree() }); - guard = new EndUserAgreementGuard(endUserAgreementService, router); + guard = new EndUserAgreementGuard(endUserAgreementService, authService, router); }); describe('canActivate', () => { @@ -39,7 +42,8 @@ describe('EndUserAgreementGuard', () => { it('should navigate the user with a redirect url', (done) => { const redirect = 'redirect/url'; guard.canActivate(undefined, Object.assign({ url: redirect })).subscribe(() => { - expect(router.navigateByUrl).toHaveBeenCalledWith(jasmine.anything(), { state: { redirect } }); + expect(authService.setRedirectUrl).toHaveBeenCalledWith(redirect); + expect(router.navigateByUrl).toHaveBeenCalled(); done(); }); }); diff --git a/src/app/core/end-user-agreement/end-user-agreement.guard.ts b/src/app/core/end-user-agreement/end-user-agreement.guard.ts index 450385984a..e42d885133 100644 --- a/src/app/core/end-user-agreement/end-user-agreement.guard.ts +++ b/src/app/core/end-user-agreement/end-user-agreement.guard.ts @@ -4,6 +4,7 @@ import { Observable } from 'rxjs/internal/Observable'; import { returnEndUserAgreementUrlTreeOnFalse } from '../shared/operators'; import { EndUserAgreementService } from './end-user-agreement.service'; import { tap } from 'rxjs/operators'; +import { AuthService } from '../auth/auth.service'; /** * A guard redirecting users to the end agreement page when they haven't accepted the latest user agreement @@ -12,6 +13,7 @@ import { tap } from 'rxjs/operators'; export class EndUserAgreementGuard implements CanActivate { constructor(protected endUserAgreementService: EndUserAgreementService, + protected authService: AuthService, protected router: Router) { } @@ -26,7 +28,8 @@ export class EndUserAgreementGuard implements CanActivate { returnEndUserAgreementUrlTreeOnFalse(this.router), tap((result) => { if (result instanceof UrlTree) { - this.router.navigateByUrl(result, { state: { redirect: state.url } }) + this.authService.setRedirectUrl(state.url); + this.router.navigateByUrl(result); } }) ); diff --git a/src/app/info/end-user-agreement/end-user-agreement.component.spec.ts b/src/app/info/end-user-agreement/end-user-agreement.component.spec.ts index 5d6b3f904c..875d6e2dbd 100644 --- a/src/app/info/end-user-agreement/end-user-agreement.component.spec.ts +++ b/src/app/info/end-user-agreement/end-user-agreement.component.spec.ts @@ -24,19 +24,19 @@ describe('EndUserAgreementComponent', () => { let redirectUrl; function init() { + redirectUrl = 'redirect/url'; + endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', { hasCurrentUserAcceptedAgreement: observableOf(false), setUserAcceptedAgreement: observableOf(true) }); notificationsService = jasmine.createSpyObj('notificationsService', ['success', 'error']); authService = jasmine.createSpyObj('authService', { - isAuthenticated: observableOf(true) + isAuthenticated: observableOf(true), + getRedirectUrl: observableOf(redirectUrl) }); store = jasmine.createSpyObj('store', ['dispatch']); router = jasmine.createSpyObj('router', ['navigate', 'navigateByUrl']); - - redirectUrl = 'redirect/url'; - window.history.pushState({ redirect: redirectUrl }, ''); } beforeEach(async(() => { diff --git a/src/app/info/end-user-agreement/end-user-agreement.component.ts b/src/app/info/end-user-agreement/end-user-agreement.component.ts index f86b0c3434..a60db6a496 100644 --- a/src/app/info/end-user-agreement/end-user-agreement.component.ts +++ b/src/app/info/end-user-agreement/end-user-agreement.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { AuthService } from '../../core/auth/auth.service'; -import { take } from 'rxjs/operators'; +import { switchMap, take } from 'rxjs/operators'; import { Router } from '@angular/router'; import { Store } from '@ngrx/store'; import { AppState } from '../../app.reducer'; @@ -8,7 +8,8 @@ import { LogOutAction } from '../../core/auth/auth.actions'; import { EndUserAgreementService } from '../../core/end-user-agreement/end-user-agreement.service'; import { NotificationsService } from '../../shared/notifications/notifications.service'; import { TranslateService } from '@ngx-translate/core'; -import { hasValue } from '../../shared/empty.util'; +import { of as observableOf } from 'rxjs'; +import { isNotEmpty } from '../../shared/empty.util'; @Component({ selector: 'ds-end-user-agreement', @@ -54,15 +55,20 @@ export class EndUserAgreementComponent implements OnInit { * Set the End User Agreement, display a notification and (optionally) redirect the user back to their original destination */ submit() { - this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).subscribe((success) => { - if (success) { - this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success')); - const redirect = window.history.state.redirect; - if (hasValue(redirect)) { - this.router.navigateByUrl(redirect); + this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).pipe( + switchMap((success) => { + if (success) { + this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success')); + return this.authService.getRedirectUrl(); + } else { + this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error')); + return observableOf(undefined); } - } else { - this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error')); + }), + take(1) + ).subscribe((redirectUrl) => { + if (isNotEmpty(redirectUrl)) { + this.router.navigateByUrl(redirectUrl); } }); }