Skip to content

Commit

Permalink
Merge pull request #92 from bcgov/SPR-190-Solve-401-Error-Keycloak-Ex…
Browse files Browse the repository at this point in the history
…pire

SPR-190 Solve 401 error keycloak expire
  • Loading branch information
dbarkowsky authored Jul 17, 2023
2 parents ca5d41f + b58a681 commit 950148a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
18 changes: 13 additions & 5 deletions app/src/components/custom/fileHandlers/FileLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,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,
});
}
}
};

Expand Down
18 changes: 13 additions & 5 deletions app/src/components/custom/forms/RequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,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,
});
}
}
};

Expand Down
20 changes: 14 additions & 6 deletions app/src/components/custom/modals/UserReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
};

Expand Down
8 changes: 7 additions & 1 deletion app/src/keycloak/KeycloakWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <div>{children}</div>;
Expand Down
18 changes: 13 additions & 5 deletions app/src/pages/IndividualRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,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,
});
}
}
};

Expand Down

0 comments on commit 950148a

Please sign in to comment.