From 2de505748555d246f92005121fdec894c3488d8f Mon Sep 17 00:00:00 2001 From: dbarkowsky Date: Fri, 14 Jul 2023 14:46:18 -0700 Subject: [PATCH 1/2] Use interval to periodically check token. --- app/src/keycloak/KeycloakWrapper.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/keycloak/KeycloakWrapper.tsx b/app/src/keycloak/KeycloakWrapper.tsx index 627d8ba..ca853d6 100644 --- a/app/src/keycloak/KeycloakWrapper.tsx +++ b/app/src/keycloak/KeycloakWrapper.tsx @@ -9,8 +9,9 @@ interface IKeycloakWrapper { const KeycloakWrapper = (props: IKeycloakWrapper) => { const { children } = props; const { setUserInfo, refreshAccessToken } = useAuthService(); + const tokenRefreshTime = 1000 * 60; // 60 seconds - useEffect(() => { + const checkToken = () => { // Get the current URL and its search parameters const url = new URL(window.location.href); const { searchParams } = url; @@ -26,6 +27,11 @@ const KeycloakWrapper = (props: IKeycloakWrapper) => { // If there is no token in the URL, try to refresh the access token refreshAccessToken(); } + }; + + useEffect(() => { + checkToken(); + setInterval(checkToken, tokenRefreshTime); }, []); return
{children}
; From b58a681484fb7d7377006102b023dedc3a7d26c9 Mon Sep 17 00:00:00 2001 From: dbarkowsky Date: Fri, 14 Jul 2023 15:00:36 -0700 Subject: [PATCH 2/2] Added more axios 401 catch cases --- .../custom/fileHandlers/FileLink.tsx | 18 ++++++++++++----- .../components/custom/forms/RequestForm.tsx | 18 ++++++++++++----- .../components/custom/modals/UserReport.tsx | 20 +++++++++++++------ app/src/pages/IndividualRequest.tsx | 18 ++++++++++++----- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/app/src/components/custom/fileHandlers/FileLink.tsx b/app/src/components/custom/fileHandlers/FileLink.tsx index 6ea7f07..6de691d 100644 --- a/app/src/components/custom/fileHandlers/FileLink.tsx +++ b/app/src/components/custom/fileHandlers/FileLink.tsx @@ -58,11 +58,19 @@ const FileLink = (props: FileLinkProps) => { const file: string = await axios(axiosReqConfig).then((response) => response.data.file); return file; } catch (e: any) { - setErrorState({ - text: 'File could not be retrieved.', - open: true, - style: errorStyles.error, - }); + if (axios.isAxiosError(e) && e.code === '401') { + setErrorState({ + text: 'User is unauthenticated. Redirecting to login.', + open: true, + }); + window.location.reload(); + } else { + setErrorState({ + text: 'File could not be retrieved.', + open: true, + style: errorStyles.error, + }); + } } }; diff --git a/app/src/components/custom/forms/RequestForm.tsx b/app/src/components/custom/forms/RequestForm.tsx index 476bd18..33a80ae 100644 --- a/app/src/components/custom/forms/RequestForm.tsx +++ b/app/src/components/custom/forms/RequestForm.tsx @@ -145,11 +145,19 @@ const RequestForm = (props: RequestFormProps) => { } } catch (e) { console.warn(e); - setErrorState({ - text: 'Deletion Unsuccessful.', - open: true, - style: errorStyles.error, - }); + if (axios.isAxiosError(e) && e.code === '401') { + setErrorState({ + text: 'User is unauthenticated. Redirecting to login.', + open: true, + }); + window.location.reload(); + } else { + setErrorState({ + text: 'Deletion Unsuccessful.', + open: true, + style: errorStyles.error, + }); + } } }; diff --git a/app/src/components/custom/modals/UserReport.tsx b/app/src/components/custom/modals/UserReport.tsx index 8bbedf0..155ff56 100644 --- a/app/src/components/custom/modals/UserReport.tsx +++ b/app/src/components/custom/modals/UserReport.tsx @@ -65,12 +65,20 @@ export const UserReport = () => { } } catch (e) { console.warn(e); - // Request failed. Report error to user. - setErrorState({ - text: 'Issue could not be submitted.', - open: true, - style: errorStyles.error, - }); + if (axios.isAxiosError(e) && e.code === '401') { + setErrorState({ + text: 'User is unauthenticated. Redirecting to login.', + open: true, + }); + window.location.reload(); + } else { + // Request failed. Report error to user. + setErrorState({ + text: 'Issue could not be submitted.', + open: true, + style: errorStyles.error, + }); + } } }; diff --git a/app/src/pages/IndividualRequest.tsx b/app/src/pages/IndividualRequest.tsx index 9fdd618..729bbb7 100644 --- a/app/src/pages/IndividualRequest.tsx +++ b/app/src/pages/IndividualRequest.tsx @@ -184,11 +184,19 @@ const IndividualRequest = () => { } } catch (e) { console.warn('Record could not be updated.'); - setErrorState({ - text: 'Update was unsuccessful.', - open: true, - style: errorStyles.error, - }); + if (axios.isAxiosError(e) && e.code === '401') { + setErrorState({ + text: 'User is unauthenticated. Redirecting to login.', + open: true, + }); + window.location.reload(); + } else { + setErrorState({ + text: 'Update was unsuccessful.', + open: true, + style: errorStyles.error, + }); + } } };