catch tenant errors
This commit is contained in:
parent
1d354ac868
commit
03084293b2
@ -4,11 +4,13 @@ import { NGXLogger } from 'ngx-logger';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
import { KeycloakStatusService } from '../services/keycloak-status.service';
|
||||
import { keycloakInitializer } from '../keycloak-initializer';
|
||||
import { TenantsService } from '../services';
|
||||
|
||||
export const ifLoggedIn: CanActivateFn = async (route: ActivatedRouteSnapshot) => {
|
||||
const logger = inject(NGXLogger);
|
||||
logger.info('[ROUTES] Check if can activate main');
|
||||
|
||||
const tenantsService = inject(TenantsService);
|
||||
const keycloakService = inject(KeycloakService);
|
||||
const keycloakStatusService = inject(KeycloakStatusService);
|
||||
|
||||
@ -24,12 +26,13 @@ export const ifLoggedIn: CanActivateFn = async (route: ActivatedRouteSnapshot) =
|
||||
logger.info('[KEYCLOAK] Keycloak init...');
|
||||
await keycloakInitializer(tenant);
|
||||
logger.info('[KEYCLOAK] Keycloak init done!');
|
||||
await tenantsService.selectTenant(tenant);
|
||||
}
|
||||
|
||||
const isLoggedIn = await keycloakService.isLoggedIn();
|
||||
|
||||
if (isLoggedIn && tenant) {
|
||||
logger.warn('[ROUTES] Is logged in, continuing');
|
||||
if (isLoggedIn) {
|
||||
logger.info('[ROUTES] Is logged in, continuing');
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -6,16 +6,22 @@ import { KeycloakService } from 'keycloak-angular';
|
||||
export const ifNotLoggedIn: CanActivateFn = async (route: ActivatedRouteSnapshot) => {
|
||||
const logger = inject(NGXLogger);
|
||||
const router = inject(Router);
|
||||
const keycloakService = inject(KeycloakService);
|
||||
|
||||
const tenant = route.paramMap.get('tenant');
|
||||
const isLoggedIn = await inject(KeycloakService).isLoggedIn();
|
||||
const isLoggedIn = await keycloakService.isLoggedIn();
|
||||
|
||||
if (isLoggedIn && !!tenant) {
|
||||
logger.warn('[ROUTES] Is logged in for ' + tenant + ', redirecting to /' + tenant);
|
||||
await router.navigate([tenant]);
|
||||
if (!isLoggedIn) {
|
||||
logger.info('[ROUTES] Not logged in, continuing to selected route');
|
||||
return true;
|
||||
}
|
||||
|
||||
const tenant = route.paramMap.get('tenant') || keycloakService.getKeycloakInstance().realm;
|
||||
if (!tenant) {
|
||||
logger.error('[ROUTES] Tenant not found in route or keycloak realm');
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.info('[ROUTES] Not logged in, continuing to select tenant page');
|
||||
return true;
|
||||
logger.warn('[ROUTES] Is logged in for ' + tenant + ', redirecting to /' + tenant);
|
||||
await router.navigate([tenant]);
|
||||
return false;
|
||||
};
|
||||
|
||||
@ -3,6 +3,8 @@ import { KeycloakOptions, KeycloakService } from 'keycloak-angular';
|
||||
import { KeycloakStatusService } from './services/keycloak-status.service';
|
||||
import { inject } from '@angular/core';
|
||||
import { getConfig } from '../services';
|
||||
import { NGXLogger } from 'ngx-logger';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
function getKeycloakOptions(baseUrl: string, config: IqserAppConfig, tenant: string): KeycloakOptions {
|
||||
return {
|
||||
@ -35,12 +37,20 @@ function configureAutomaticRedirectToLoginScreen(keyCloakService: KeycloakServic
|
||||
}
|
||||
|
||||
export async function keycloakInitializer(tenant: string) {
|
||||
const logger = inject(NGXLogger);
|
||||
const router = inject(Router);
|
||||
const keycloakService = inject(KeycloakService);
|
||||
const keycloakStatusService = inject(KeycloakStatusService);
|
||||
const baseHref = inject(BASE_HREF);
|
||||
const config = getConfig();
|
||||
|
||||
const keycloakOptions = getKeycloakOptions(baseHref, config, tenant);
|
||||
await keycloakService.init(keycloakOptions);
|
||||
try {
|
||||
await keycloakService.init(keycloakOptions);
|
||||
} catch (error) {
|
||||
logger.error('[KEYCLOAK] Unable to initialize Keycloak', error);
|
||||
await router.navigate(['/']);
|
||||
return;
|
||||
}
|
||||
configureAutomaticRedirectToLoginScreen(keycloakService, keycloakStatusService);
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ export class KeycloakStatusService {
|
||||
idpHint: this.#config.OAUTH_IDP_HINT,
|
||||
});
|
||||
|
||||
this.#logger.error('[KEYCLOAK] Redirect to login url: ', url);
|
||||
this.#logger.info('[KEYCLOAK] Redirect to login url: ', url);
|
||||
|
||||
window.location.href = url;
|
||||
} else {
|
||||
|
||||
@ -42,7 +42,9 @@ export class TenantsService {
|
||||
const tenant = this.getTenantFromRoute();
|
||||
if (tenant) {
|
||||
this.#logger.info('[TENANTS] Tenant from route: ', tenant);
|
||||
await this.selectTenant(tenant);
|
||||
if (await this.selectTenant(tenant)) {
|
||||
await this.#router.navigate([tenant]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@ -50,7 +52,10 @@ export class TenantsService {
|
||||
|
||||
if (!this.hasMultipleTenants) {
|
||||
this.#logger.info('[TENANTS] Only one tenant loaded, auto-select it and redirect to login page');
|
||||
await this.selectTenant(tenants[0].tenantId);
|
||||
const tenant = tenants[0].tenantId;
|
||||
if (await this.selectTenant(tenant)) {
|
||||
await this.#router.navigate([tenant]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,12 +76,12 @@ export class TenantsService {
|
||||
if (unknownTenant) {
|
||||
this.#logger.info('[TENANTS] Unknown tenant, redirecting to select tenant page');
|
||||
await this.#router.navigate(['/']);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
this.#mutateStorage(tenantId);
|
||||
this.setCurrentTenantId(tenantId);
|
||||
await this.#router.navigate([tenantId]);
|
||||
return true;
|
||||
}
|
||||
|
||||
setCurrentTenantId(tenantId: string) {
|
||||
|
||||
@ -62,6 +62,7 @@ export abstract class IqserUserService<
|
||||
await this._cacheApiService.wipeCaches();
|
||||
await this._keycloakService.logout(this._keycloakStatusService.createLoginUrl());
|
||||
} catch (e) {
|
||||
console.log('Logout failed: ', e);
|
||||
await this.redirectToLogin();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user