Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
"@babel/core": "^7.17.10",
"@babel/eslint-parser": "^7.17.0",
"@folio/eslint-config-stripes": "^7.0.0",
"@folio/stripes": "^9.1.0",
"@folio/stripes": "^9.2.0",
"@folio/stripes-cli": "^3.0.0",
"@formatjs/cli": "^6.1.3",
"eslint": "^7.32.0",
Expand Down
39 changes: 19 additions & 20 deletions src/settings/RefreshTokenRotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,41 @@ import { useCallback, useEffect, useState } from 'react';
import { Field, Form } from 'react-final-form';
import { FormattedMessage } from 'react-intl';

import { getTokenExpiry } from '@folio/stripes/core';
import { Button, LoadingPane, Pane, PaneHeader, TextField } from '@folio/stripes/components';
import { getTokenExpiry, RTR_CONSTANTS } from '@folio/stripes/core';

/**
* manipulate AT/RT expiration dates in storage in order to test RTR.
* @returns
*/
const RefreshTokenRotation = ({ stripes }) => {
// why WHY copy this string here instead of importing it from stripes-core?
//
// RTR_FORCE_REFRESH_EVENT will be present in stripes-core 10.2.0 (stripes
// 9.2.0). Importing it would force the stripes peer depedency to bump from
// ^9.1.0 to ^9.2.0.If we copy the string instead of importing it, we can
// remain compatible with 9.1.0.
//
// OK, compatibility is nice. But it's still gross, right? Yep, super gross.
// Aren't you nauseated? Yes, yes I am. 🤢🧼🛁
const RTR_FORCE_REFRESH_EVENT = '@folio/stripes/core::RTRForceRefresh';

const [isLoading, setIsLoading] = useState(true);
const [tokenExpiration, setTokenExpiration] = useState({});
const [tokenExpiration, setTokenExpiration] = useState({ atExpires: -1, rtExpires: -1 });

useEffect(() => {
setIsLoading(true);
getTokenExpiry().then((te) => {
setTokenExpiration(te ?? { atExpires: -1, rtExpires: -1 });
setIsLoading(false);
});
const callback = () => {
setIsLoading(true);
getTokenExpiry().then((te) => {
setTokenExpiration({
atExpires: te.atExpires ?? te.accessTokenExpiration ?? -1,
rtExpires: te.rtExpires ?? te.refreshTokenExpiration ?? -1,
});
setIsLoading(false);
});
};

callback();

window.addEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback);

return () => window.removeEventListener(RTR_CONSTANTS.RTR_SUCCESS_EVENT, callback);
}, []);

/**
* forceRefresh
* dispatch an event to force a token rotation
*/
const forceRefresh = useCallback(
() => window.dispatchEvent(new Event(RTR_FORCE_REFRESH_EVENT)),
() => window.dispatchEvent(new Event(RTR_CONSTANTS.RTR_FORCE_REFRESH_EVENT)),
[],
);

Expand Down