diff --git a/src/app/core/data/dso-redirect.service.spec.ts b/src/app/core/data/dso-redirect.service.spec.ts index 9271bd5f7f4..833ae522998 100644 --- a/src/app/core/data/dso-redirect.service.spec.ts +++ b/src/app/core/data/dso-redirect.service.spec.ts @@ -13,6 +13,8 @@ import { EMBED_SEPARATOR } from './base/base-data.service'; import { HardRedirectService } from '../services/hard-redirect.service'; import { environment } from '../../../environments/environment.test'; import { AppConfig } from '../../../config/app-config.interface'; +import { AuthService } from '../auth/auth.service'; +import { RouterStub } from '../../shared/testing/router.stub'; describe('DsoRedirectService', () => { let scheduler: TestScheduler; @@ -22,6 +24,8 @@ describe('DsoRedirectService', () => { let rdbService: RemoteDataBuildService; let redirectService: HardRedirectService; let remoteData; + let router: any; + let authService: AuthService; const dsoUUID = '9b4f22f4-164a-49db-8817-3316b6ee5746'; const dsoHandle = '1234567789/22'; const encodedHandle = encodeURIComponent(dsoHandle); @@ -57,13 +61,21 @@ describe('DsoRedirectService', () => { redirect: {} }); + router = new RouterStub(); + + authService = jasmine.createSpyObj('authService', { + setRedirectUrl: {} + }); + service = new DsoRedirectService( environment as AppConfig, requestService, rdbService, objectCache, halService, - redirectService + redirectService, + router, + authService ); }); diff --git a/src/app/core/data/dso-redirect.service.ts b/src/app/core/data/dso-redirect.service.ts index 4585df5b4bd..cea885c7651 100644 --- a/src/app/core/data/dso-redirect.service.ts +++ b/src/app/core/data/dso-redirect.service.ts @@ -22,6 +22,8 @@ import { IdentifiableDataService } from './base/identifiable-data.service'; import { getDSORoute } from '../../app-routing-paths'; import { HardRedirectService } from '../services/hard-redirect.service'; import { APP_CONFIG, AppConfig } from '../../../config/app-config.interface'; +import { Router } from '@angular/router'; +import { AuthService } from '../auth/auth.service'; const ID_ENDPOINT = 'pid'; const UUID_ENDPOINT = 'dso'; @@ -76,7 +78,9 @@ export class DsoRedirectService { protected rdbService: RemoteDataBuildService, protected objectCache: ObjectCacheService, protected halService: HALEndpointService, - private hardRedirectService: HardRedirectService + private hardRedirectService: HardRedirectService, + private router: Router, + private authService: AuthService, ) { this.dataService = new DsoByIdOrUUIDDataService(requestService, rdbService, objectCache, halService); } @@ -104,6 +108,15 @@ export class DsoRedirectService { } } } + // Redirect to login page if the user is not authenticated to see the requested page + if (response.hasFailed && (response.statusCode === 401 || response.statusCode === 403)) { + // Remove `/` from the namespace if it is empty + const namespace = this.appConfig.ui.nameSpace === '/' ? '' : this.appConfig.ui.nameSpace; + // Compose redirect URL - remove `https://.../namespace` from the current URL. Keep only `handle/...` + const redirectUrl = window.location.href.replace(this.appConfig.ui.baseUrl + namespace, ''); + this.authService.setRedirectUrl(redirectUrl); + this.router.navigateByUrl('login'); + } }) ); }