I have a situation that goes like this :
I have a php project that I made from scratch serving as an API endpoint to a client web app created with React. For now they are both locally, php running on localhost:8000
and react on localhost:3000
.
The issue appears when I try to make token protected request using axios. The php generated session is lost after login.
I've tried to add withCredentials:true
client side and also enable
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
in the server side as many of the accpeted answers here mentioned but did not have no luck.
PHP headers :
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With, Set-Cookie, Cookie, Bearer');
Axios request:
return axios({
method: 'GET',
url: LOAD_NOTES_ENDPOINT,
headers: {'Authorization': token, 'Access-Control-Allow-Origin': '*',}
})
.then(response => {
console.log(response.data);
return response;
})
.catch(
(err) => {
return err.response;
}
);
Currently I'm able to send generate token on login and send it back when trying to access protected route and this works fine if I make the request on Postman and this confirmed that this is strictly a CORS issue. I've struggled for 2 days now to find a solution and opened every possible link on the web regarding this issue but all of them are are proposing things I've already tried and didn't work. I could find a workaround but that would leave the server open to CSRF attacks and that is something that I have to prevent.
I'm I missing something here? Did anyone else find a solution about this?