Skip to content

Commit 4e1f1ff

Browse files
committed
Changed app context logic to get CSRF value and reuse it if available
1 parent 179e1c6 commit 4e1f1ff

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

web-ui/src/context/AppContext.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import {getCertifications} from "../api/certification.js";
3434

3535
const AppContext = React.createContext();
3636

37+
function getSessionCookieValue(name) {
38+
const cookies = document.cookie.split(';');
39+
for (let i = 0; i < cookies.length; i++) {
40+
const cookie = cookies[i].trim();
41+
if (cookie.startsWith(name + '=')) {
42+
return decodeURIComponent(cookie.substring(name.length + 1));
43+
}
44+
}
45+
return null;
46+
}
47+
3748
const AppContextProvider = props => {
3849
const [state, dispatch] = useReducer(
3950
reducer,
@@ -63,14 +74,17 @@ const AppContextProvider = props => {
6374
const url = `${BASE_API_URL}/csrf/cookie`;
6475
useEffect(() => {
6576
const getCsrf = async () => {
66-
if (!csrf) {
77+
const cookieVal = getSessionCookieValue('_csrf');
78+
if (!csrf && !cookieVal) {
6779
const res = await fetch(url, {
6880
responseType: 'text',
6981
credentials: 'include'
7082
});
7183
if (res && res.ok) {
7284
dispatch({ type: SET_CSRF, payload: await res.text() });
7385
}
86+
} else if (cookieVal) {
87+
dispatch({ type: SET_CSRF, payload: cookieVal });
7488
}
7589
};
7690
getCsrf();

0 commit comments

Comments
 (0)