Skip to content

Commit

Permalink
Merge pull request #1574 from tidepool-org/UPLOAD-1123-session-launch
Browse files Browse the repository at this point in the history
[UPLOAD-1123] prevent hashchange events when authenticated
  • Loading branch information
krystophv authored Aug 14, 2023
2 parents 56da494 + 7c0b098 commit bf5bb36
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
44 changes: 36 additions & 8 deletions app/keycloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const updateKeycloakConfig = (info, store) => {
}
};

let latestKeycloakEvent = null;

const onKeycloakEvent = (store) => (event, error) => {
latestKeycloakEvent = event;
switch (event) {
case 'onReady': {
let logoutUrl = keycloak.createLogoutUrl({
Expand Down Expand Up @@ -116,7 +119,7 @@ const onKeycloakTokens = (store) => (tokens) => {
if (tokens.token !== keycloak?.token) {
if (rollbar) {
rollbar.info('keycloak token mismatch', {
keycloakToken: jose.decodeJwt(keycloak?.token),
keycloakToken: keycloak?.token ? jose.decodeJwt(keycloak?.token) : {},
tokensToken: tokenParsed,
});
}
Expand All @@ -137,6 +140,17 @@ const onKeycloakTokens = (store) => (tokens) => {
return;
}
} else {
const expectedEvents = [
'onReady',
'onAuthLogout',
'onAuthRefreshError',
'onAuthError',
'onInitError'
];
if (expectedEvents.includes(latestKeycloakEvent)) {
return;
}

// if we don't have a token, we can't save the session
if(rollbar) {
rollbar.error('keycloak token missing', {
Expand Down Expand Up @@ -234,13 +248,17 @@ export const KeycloakWrapper = (props) => {
// incrementing externally defined `key` forces unmount/remount as provider doesn't expect to
// have the authClient refreshed and only sets up refresh timeout on mount
const onHashChange = useCallback(() => {
keycloak = new Keycloak({
url: keycloakConfig.url,
realm: keycloakConfig.realm,
clientId: 'tidepool-uploader-sso',
});
keyCount++;
forceUpdate();
// we only want to do this when unauthenticated since people can hit the
// launch button multiple times
if (!keycloak?.authenticated) {
keycloak = new Keycloak({
url: keycloakConfig.url,
realm: keycloakConfig.realm,
clientId: 'tidepool-uploader-sso',
});
keyCount++;
forceUpdate();
}
}, [keycloakConfig.realm, keycloakConfig.url, blipRedirect]);

useEffect(() => {
Expand All @@ -250,6 +268,16 @@ export const KeycloakWrapper = (props) => {
};
}, [onHashChange]);

// clear the refresh timeout on unmount
useEffect(() => {
return () => {
if (refreshTimeout) {
clearTimeout(refreshTimeout);
refreshTimeout = null;
}
};
}, []);

if (keycloakConfig.url && keycloakConfig.instantiated && blipRedirect) {
return (
<ReactKeycloakProvider
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tidepool-uploader",
"productName": "tidepool-uploader",
"version": "2.54.1",
"version": "2.54.2-session-launch.2",
"description": "Tidepool Project Universal Uploader",
"main": "./main.prod.js",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tidepool-uploader",
"version": "2.54.1",
"version": "2.54.2-session-launch.2",
"description": "Tidepool Project Universal Uploader",
"private": true,
"main": "main.prod.js",
Expand Down

0 comments on commit bf5bb36

Please sign in to comment.