From ec77117f3675f8a6acc7a475662e9bd3c7db65f8 Mon Sep 17 00:00:00 2001 From: Sofia Papacharalampous Date: Wed, 10 Jul 2024 11:03:09 +0300 Subject: [PATCH] fixed redirect on wrong tenant url --- frontend/src/app/app.module.ts | 14 +++++++++++--- .../src/app/core/services/auth/auth.service.ts | 1 - frontend/src/app/ui/auth/login/login.component.ts | 3 +++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/app.module.ts b/frontend/src/app/app.module.ts index cb02a6f63..5950e4efc 100644 --- a/frontend/src/app/app.module.ts +++ b/frontend/src/app/app.module.ts @@ -43,6 +43,7 @@ import { TenantHandlingService } from './core/services/tenant/tenant-handling.se import { GuidedTourModule } from './library/guided-tour/guided-tour.module'; import { DepositOauth2DialogModule } from './ui/misc/deposit-oauth2-dialog/deposit-oauth2-dialog.module'; import { OpenCDMPCustomTranslationCompiler } from './utilities/translate/opencdmp-custom-translation-compiler'; +import { Router } from '@angular/router'; // AoT requires an exported function for factories export function HttpLoaderFactory(languageHttpService: LanguageHttpService) { @@ -79,7 +80,7 @@ const cookieConfig: NgcCookieConsentConfig = { type: 'info' }; -export function InstallationConfigurationFactory(appConfig: ConfigurationService, keycloak: KeycloakService, authService: AuthService, languageService: LanguageService, tenantHandlingService: TenantHandlingService) { +export function InstallationConfigurationFactory(appConfig: ConfigurationService, keycloak: KeycloakService, authService: AuthService, languageService: LanguageService, tenantHandlingService: TenantHandlingService, router: Router) { return () => appConfig.loadConfiguration().then(() => { return languageService.loadAvailableLanguages().toPromise(); }).then(x => keycloak.init({ @@ -109,7 +110,14 @@ export function InstallationConfigurationFactory(appConfig: ConfigurationService const tenantCode = tenantHandlingService.extractTenantCodeFromUrlPath(window.location.pathname) ?? authService.selectedTenant() ?? 'default'; const tokenPromise = keycloak.getToken(); - return authService.prepareAuthRequest(from(tokenPromise), tenantCode, { params }).toPromise().catch(error => authService.onAuthenticateError(error)); + return authService.prepareAuthRequest(from(tokenPromise), tenantCode, { params }) + .toPromise() + .then(() => { + if (authService.selectedTenant() != tenantCode) { + router.navigate(['/']); + } + }) + .catch(error => authService.onAuthenticateError(error)); })); } @@ -157,7 +165,7 @@ export function InstallationConfigurationFactory(appConfig: ConfigurationService { provide: APP_INITIALIZER, useFactory: InstallationConfigurationFactory, - deps: [ConfigurationService, KeycloakService, AuthService, LanguageService, TenantHandlingService], + deps: [ConfigurationService, KeycloakService, AuthService, LanguageService, TenantHandlingService, Router], multi: true }, { diff --git a/frontend/src/app/core/services/auth/auth.service.ts b/frontend/src/app/core/services/auth/auth.service.ts index d6ac68894..e6458d6dd 100644 --- a/frontend/src/app/core/services/auth/auth.service.ts +++ b/frontend/src/app/core/services/auth/auth.service.ts @@ -174,7 +174,6 @@ export class AuthService extends BaseService { return this.ensureTenant(tenantCode ?? this.selectedTenant() ?? 'default'); }), concatMap(response => { - if (response == 'default' && tenantCode != 'default') throw Error(); return this.principalService.me(httpParams); }), concatMap(response => { diff --git a/frontend/src/app/ui/auth/login/login.component.ts b/frontend/src/app/ui/auth/login/login.component.ts index 1c552c62c..a3ed7065d 100644 --- a/frontend/src/app/ui/auth/login/login.component.ts +++ b/frontend/src/app/ui/auth/login/login.component.ts @@ -40,6 +40,9 @@ export class LoginComponent extends BaseComponent implements OnInit { this.authService.prepareAuthRequest(from(this.keycloakService.getToken()), tenantCode).pipe(takeUntil(this._destroyed)).subscribe( () => { let returnUrL = this.returnUrl; + + if (this.authService.selectedTenant() != tenantCode) returnUrL = this.routerUtils.generateUrl('/'); + this.zone.run(() => this.router.navigateByUrl(this.routerUtils.generateUrl(returnUrL))); }, (error) => this.authService.authenticate('/'));