diff --git a/patientsearch/src/js/constants/consts.js b/patientsearch/src/js/constants/consts.js index 35b4055a..a9bbbf6e 100644 --- a/patientsearch/src/js/constants/consts.js +++ b/patientsearch/src/js/constants/consts.js @@ -155,3 +155,7 @@ export const REALM_ACCESS_TOKEN_KEY = "realm_access"; export const MAX_MAIN_TABLE_WIDTH = "1280px"; export const FOLLOWING_FLAG = "following"; export const MIN_QUERY_COUNT = 500; +export const HTTP_FORBIDDEN_STATUS_CODE = 403; +export const HTTP_UNAUTHORIZED_STATUS_CODE = 401; +export const FORBIDDEN_LOGOUT_URL = "/logout?forbidden=true"; +export const UNAUTHORIZED_LOGOUT_URL = "/logout?unauthorized=true"; diff --git a/patientsearch/src/js/context/PatientListContextProvider.js b/patientsearch/src/js/context/PatientListContextProvider.js index 0d35e2d2..7c49247e 100644 --- a/patientsearch/src/js/context/PatientListContextProvider.js +++ b/patientsearch/src/js/context/PatientListContextProvider.js @@ -263,14 +263,14 @@ export default function PatientListContextProvider({ children }) { setNoDataText(text); }; const handleErrorCallback = (e) => { - if (e && e.status === 401) { - setErrorMessage("Unauthorized."); - window.location = "/logout?unauthorized=true"; + if (e && e.status === constants.HTTP_UNAUTHORIZED_STATUS_CODE) { + setErrorMessage("Unauthorized. Logging out..."); + window.location = constants.UNAUTHORIZED_LOGOUT_URL; return; } - if (e && e.status === 403) { - setErrorMessage("Forbidden."); - window.location = "/logout?forbidden=true"; + if (e && e.status === constants. HTTP_FORBIDDEN_STATUS_CODE) { + setErrorMessage("Forbidden. Logging out..."); + window.location = constants.FORBIDDEN_LOGOUT_URL; return; } setErrorMessage( diff --git a/patientsearch/src/js/context/UserContextProvider.js b/patientsearch/src/js/context/UserContextProvider.js index ffae13e2..57aca486 100644 --- a/patientsearch/src/js/context/UserContextProvider.js +++ b/patientsearch/src/js/context/UserContextProvider.js @@ -10,9 +10,15 @@ import { getRolesFromToken, getAccessToken, isString, - validateToken, + validateToken } from "../helpers/utility"; -import { noCacheParam } from "../constants/consts"; +import { + noCacheParam, + HTTP_FORBIDDEN_STATUS_CODE, + HTTP_UNAUTHORIZED_STATUS_CODE, + FORBIDDEN_LOGOUT_URL, + UNAUTHORIZED_LOGOUT_URL, + } from "../constants/consts"; const UserContext = React.createContext({}); /* * context component that allows user info to be accessible to its children component(s) @@ -21,9 +27,13 @@ export default function UserContextProvider({ children }) { const [user, setUser] = useState(null); const [errorMessage, setErrorMessage] = useState(""); const handleErrorCallback = (e) => { - if (parseInt(e) === 401) { - setErrorMessage("Unauthorized"); - window.location = "/logout?unauthorized=true"; + const status = parseInt(e?.status); + if (status === HTTP_UNAUTHORIZED_STATUS_CODE) { + window.location = UNAUTHORIZED_LOGOUT_URL; + return; + } + if (status === HTTP_FORBIDDEN_STATUS_CODE) { + window.location = FORBIDDEN_LOGOUT_URL; return; } setErrorMessage( @@ -31,7 +41,7 @@ export default function UserContextProvider({ children }) { ? e : e && e.message ? e.message - : "Error occurred processing user data" + : "Error occurred processing requested data" ); }; useEffect(() => { @@ -105,12 +115,12 @@ export default function UserContextProvider({ children }) { }, (e) => { console.log("token validation error ", e); - handleErrorCallback(401); + handleErrorCallback(e); } ); }, []); return ( - + {({ user, userError }) => { if (user || userError) return children;