I'm trying to save my a token in redux reducer. console.log works just fine, code is running and the token shows up.
when I try to save the result in a variable - async code does not run, and variable remains undefined.
this is my reducer code:
import { generateCSRF } from '../components/System/CSRF';
const csrfReducer = async (state = null, action) => {
switch(action.type){
case 'CREATE_CSRF_TOKEN':
return (
async () => {
state = await generateCSRF(); // I'm trying to save the returned token in state variable, unsuccesfully.
}
);
default:
return (state);
}
}
export default csrfReducer;
here is my async function:
import { config } from '../../config/global-config';
export const generateCSRF = async () => {
let fetchGetResponse = await fetch(`${config.api_url}csrf`, {
method: 'GET',
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
credentials: "include",
mode: 'cors'
});
let parsedResponse = await fetchGetResponse.json();
console.log(parsedResponse.csrfToken) // this line works.
return parsedResponse.csrfToken;
}