Skip to content

Commit

Permalink
include all required headers in rtr request
Browse files Browse the repository at this point in the history
without an AT, there is no way to derive the tenant so it must be
pulled from the request and explicitly passed in the rtr request via the
`x-okapi-tenant` header.
  • Loading branch information
zburke committed Oct 8, 2023
1 parent f5bce98 commit f890f5f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class StripesCore extends Component {
this.store = configureStore(initialState, this.logger, this.epics);
this.actionNames = gatherActions();

registerServiceWorker(okapiConfig.url, this.logger);
registerServiceWorker(okapiConfig, this.logger);
}

componentWillUnmount() {
Expand Down
14 changes: 10 additions & 4 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ let tokenExpiration = null;
/** string FQDN including protocol, e.g. https://some-okapi.somewhere.org */
let okapiUrl = null;

let okapiTenant = null;

/** categorical logger object */
let logger = null;

Check warning on line 14 in src/service-worker.js

View workflow job for this annotation

GitHub Actions / build-npm

'logger' is assigned a value but never used. Allowed unused vars must match /React/u

Expand Down Expand Up @@ -109,6 +111,10 @@ const rtr = async (event) => {

isRotating = true;
return fetch(`${okapiUrl}/authn/refresh`, {
headers: {
'content-type': 'application/json',
'x-okapi-tenant': okapiTenant,
},
method: 'POST',
credentials: 'include',
mode: 'cors',
Expand Down Expand Up @@ -332,14 +338,14 @@ self.addEventListener('activate', async (event) => {

/**
* eventListener: message
* listen for messages from clients and dispatch them accordingly.
* OKAPI_URL: store
* listen for messages from @folio/stripes-core clients and dispatch them accordingly.
*/
self.addEventListener('message', async (event) => {
if (event.data.source === '@folio/stripes-core') {
console.info('-- (rtr-sw) reading', event.data);
if (event.data.type === 'OKAPI_URL') {
okapiUrl = event.data.value;
if (event.data.type === 'OKAPI_CONFIG') {
okapiUrl = event.data.value.url;
okapiTenant = event.data.value.tenant;
}

if (event.data.type === 'LOGGER') {
Expand Down
6 changes: 3 additions & 3 deletions src/serviceWorkerRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* immediately claims control. Otherwise, no RTR would occur until after a
* reload.
*
* @param {string} okapiUrl
* @param {object} okapi config object
* @param {function} callback function to call when receiving any message
* @return void
*/
export const registerServiceWorker = async (okapiUrl, logger) => {
export const registerServiceWorker = async (okapiConfig, logger) => {
if ('serviceWorker' in navigator) {
try {
let sw = null;
Expand Down Expand Up @@ -42,7 +42,7 @@ export const registerServiceWorker = async (okapiUrl, logger) => {
//
if (sw) {
logger.log('rtr', '<= sending OKAPI_URL', sw);
sw.postMessage({ source: '@folio/stripes-core', type: 'OKAPI_URL', value: okapiUrl });
sw.postMessage({ source: '@folio/stripes-core', type: 'OKAPI_CONFIG', value: okapiConfig });
logger.log('rtr', '<= sending LOGGER');
sw.postMessage({ source: '@folio/stripes-core', type: 'LOGGER', value: logger });
} else {
Expand Down

0 comments on commit f890f5f

Please sign in to comment.