Skip to content

Commit

Permalink
STCOR-900: (ECS) Automatically logged into Central tenant after loggi…
Browse files Browse the repository at this point in the history
…ng out from Member tenant (#1550)

Refs STCOR-900.

(cherry picked from commit d2cc819)
  • Loading branch information
aidynoJ authored and zburke committed Oct 31, 2024
1 parent 0d1aa47 commit 381a4e1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 };
Expand Down
12 changes: 11 additions & 1 deletion src/loginServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 14 additions & 1 deletion src/loginServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
updateUser,
validateUser,
IS_LOGGING_OUT,
SESSION_NAME
SESSION_NAME, getStoredTenant
} from './loginServices';

import {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 381a4e1

Please sign in to comment.