File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed
Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,17 @@ import {getCertifications} from "../api/certification.js";
3434
3535const 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+
3748const 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 ( ) ;
You can’t perform that action at this time.
0 commit comments