Skip to content

Commit

Permalink
fix user token check
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy Chen committed Feb 6, 2025
1 parent 1010cb4 commit 1191749
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
4 changes: 4 additions & 0 deletions patientsearch/src/js/constants/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
12 changes: 6 additions & 6 deletions patientsearch/src/js/context/PatientListContextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 18 additions & 8 deletions patientsearch/src/js/context/UserContextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -21,17 +27,21 @@ 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(
isString(e)
? e
: e && e.message
? e.message
: "Error occurred processing user data"
: "Error occurred processing requested data"
);
};
useEffect(() => {
Expand Down Expand Up @@ -105,12 +115,12 @@ export default function UserContextProvider({ children }) {
},
(e) => {
console.log("token validation error ", e);
handleErrorCallback(401);
handleErrorCallback(e);
}
);
}, []);
return (
<UserContext.Provider value={{ user: user, userError: errorMessage }}>
<UserContext.Provider value={{ user: user, userError: errorMessage}}>
<UserContext.Consumer>
{({ user, userError }) => {
if (user || userError) return children;
Expand Down

0 comments on commit 1191749

Please sign in to comment.