Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix CORS error with credentials: include?

Another question about CORS, I looked through a lot of information, but couldn't do anything. So I will be grateful for the help.

I am sending a fetch request with credentials enabled. What the browser regularly swears on at Access-Control-Allow-Credentials. I tried to configure a proxy, but it also does not work with it. So I need to add Access-Control-Allow-Credentials in response settings on the server. But I don't realize how.
I'm using create-react-app. There is not even a file with the familiar server code

error:

 The value of the 'Access-Control-Allow-Credentials' header in the response is ''
 which must be 'true' when the request's credentials mode is 'include'

Response headers:

access-control-allow-origin: http://localhost:3000

Request headers:

WARNING Provisional headers are shown
Access-Control-Request-Headers: access-control-allow-credentials,access-control-allow-origin
Access-Control-Request-Method: GET
Origin: http://localhost:3000
Referer: http://localhost:3000/
like image 738
Max Avatar asked Aug 31 '25 10:08

Max


1 Answers

Hi use the following code in your server.js or app.js in node.

var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());
app.all('*', function (req, res) {
   res.header("Access-Control-Allow-Origin", "*");
 res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
 res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");```
like image 86
Mary Avatar answered Sep 02 '25 23:09

Mary