Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('endUserAgreementGuard', () => {

beforeEach(() => {
endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', {
hasCurrentUserAcceptedAgreement: of(true),
hasCurrentUserOrCookieAcceptedAgreement: of(true),
});

router = jasmine.createSpyObj('router', {
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('endUserAgreementGuard', () => {

describe('when the user hasn\'t accepted the agreement', () => {
beforeEach(() => {
(endUserAgreementService.hasCurrentUserAcceptedAgreement as jasmine.Spy).and.returnValue(of(false));
(endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement as jasmine.Spy).and.returnValue(of(false));
});

it('should return a UrlTree', (done) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { inject } from '@angular/core';
import { CanActivateFn } from '@angular/router';
import { APP_CONFIG } from '@dspace/config/app-config.interface';
import { of } from 'rxjs';
import { take } from 'rxjs/operators';

import { endUserAgreementGuard } from './end-user-agreement.guard';
import { EndUserAgreementService } from './end-user-agreement.service';
Expand All @@ -20,6 +21,10 @@ export const endUserAgreementCurrentUserGuard: CanActivateFn =
return of(true);
}

return endUserAgreementService.hasCurrentUserAcceptedAgreement(true);
// Use hasCurrentUserOrCookieAcceptedAgreement to leverage synchronous cookie check
// This prevents guard hangs after PATCH operations when store cache may be stale
return endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement(true).pipe(
take(1),
);
},
);
10 changes: 9 additions & 1 deletion src/app/core/end-user-agreement/end-user-agreement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class EndUserAgreementService {
switchMap((authenticated) => {
if (authenticated) {
return this.authService.getAuthenticatedUserFromStore().pipe(
take(1),
map((user) => hasValue(user) && user.hasMetadata(END_USER_AGREEMENT_METADATA_FIELD) && user.firstMetadata(END_USER_AGREEMENT_METADATA_FIELD).value === 'true'),
);
} else {
Expand Down Expand Up @@ -84,7 +85,14 @@ export class EndUserAgreementService {
return this.ePersonService.patch(user, [operation]);
}),
getFirstCompletedRemoteData(),
map((response) => response.hasSucceeded),
map((response) => {
const success = response.hasSucceeded;
// Set cookie as synchronous fallback to prevent guard hangs after PATCH
if (success) {
this.setCookieAccepted(accepted);
}
return success;
}),
);
} else {
this.setCookieAccepted(accepted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export class EndUserAgreementComponent implements OnInit {
* Initialize the "accepted" property of this component by checking if the current user has accepted it before
*/
initAccepted() {
this.endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement(false).subscribe((accepted) => {
this.endUserAgreementService.hasCurrentUserOrCookieAcceptedAgreement(false).pipe(
take(1),
).subscribe((accepted) => {
this.accepted = accepted;
});
}
Expand Down
Loading