From 381a4e185c6d1bfe2b368de99af77b6ad94dfa78 Mon Sep 17 00:00:00 2001 From: aidynoJ <121284650+aidynoJ@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:02:45 +0500 Subject: [PATCH] STCOR-900: (ECS) Automatically logged into Central tenant after logging out from Member tenant (#1550) Refs STCOR-900. (cherry picked from commit d2cc8196300008829dc6bb3aa021aaec2cc07070) --- CHANGELOG.md | 1 + src/App.js | 4 ++-- src/loginServices.js | 12 +++++++++++- src/loginServices.test.js | 15 ++++++++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 665cdc4b7..b51a0ff1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 10.2.2 IN PROGRESS * Conditionally use `/users-keycloak/_self` endpoint when `users-keycloak` interface is present. Refs STCOR-835. +* Send the stored central tenant name in the header on logout. Refs STCOR-900. ## [10.2.1](https://github.com/folio-org/stripes-core/tree/v10.2.1) (2024-10-30) [Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.2.0...v10.2.1) diff --git a/src/App.js b/src/App.js index 9f2605d26..7f72ce325 100644 --- a/src/App.js +++ b/src/App.js @@ -13,6 +13,7 @@ import css from './components/SessionEventContainer/style.css'; import Root from './components/Root'; import { eventsPortal } from './constants'; +import { getStoredTenant } from './loginServices'; const StrictWrapper = ({ children }) => { if (config.disableStrictMode) { @@ -35,8 +36,7 @@ export default class StripesCore extends Component { constructor(props) { super(props); - const storedTenant = localStorage.getItem('tenant'); - const parsedTenant = storedTenant ? JSON.parse(storedTenant) : undefined; + const parsedTenant = getStoredTenant(); const okapi = (typeof okapiConfig === 'object' && Object.keys(okapiConfig).length > 0) ? { ...okapiConfig, tenant: parsedTenant?.tenantName || okapiConfig.tenant, clientId: parsedTenant?.clientId || okapiConfig.clientId } : { withoutOkapi: true }; diff --git a/src/loginServices.js b/src/loginServices.js index 060f52dd4..9216bedb2 100644 --- a/src/loginServices.js +++ b/src/loginServices.js @@ -131,6 +131,12 @@ export const setUnauthorizedPathToSession = (pathname) => { }; export const getUnauthorizedPathFromSession = () => sessionStorage.getItem(UNAUTHORIZED_PATH); +const TENANT_LOCAL_STORAGE_KEY = 'tenant'; +export const getStoredTenant = () => { + const storedTenant = localStorage.getItem(TENANT_LOCAL_STORAGE_KEY); + return storedTenant ? JSON.parse(storedTenant) : undefined; +}; + // export config values for storing user locale export const userLocaleConfig = { 'configName': 'localeSettings', @@ -500,7 +506,11 @@ export async function logout(okapiUrl, store, queryClient) { method: 'POST', mode: 'cors', credentials: 'include', - headers: getHeaders(store.getState()?.okapi?.tenant), + /* Since the tenant in the x-okapi-token and the x-okapi-tenant header on logout should match, + switching affiliations updates store.okapi.tenant, leading to mismatched tenant names from the token. + Use the tenant name stored during login to ensure they match. + */ + headers: getHeaders(getStoredTenant()?.tenantName || store.getState()?.okapi?.tenant), }) : Promise.resolve(); diff --git a/src/loginServices.test.js b/src/loginServices.test.js index 731625eb7..16163936c 100644 --- a/src/loginServices.test.js +++ b/src/loginServices.test.js @@ -23,7 +23,7 @@ import { updateUser, validateUser, IS_LOGGING_OUT, - SESSION_NAME + SESSION_NAME, getStoredTenant } from './loginServices'; import { @@ -685,4 +685,17 @@ describe('unauthorizedPath functions', () => { expect(getUnauthorizedPathFromSession()).toBe(value); }); }); + + describe('getStoredTenant', () => { + afterEach(() => { + localStorage.clear(); + }); + it('retrieves the value from localstorage', () => { + const value = { tenantName: 'diku', clientId: 'diku-id' }; + localStorage.setItem('tenant', JSON.stringify(value)); + const parsedTenant = getStoredTenant(); + + expect(parsedTenant).toStrictEqual(value); + }); + }); });