'How to pass cookies through CORS?

I have a project that sends HTTP requests from the client using Axios

axios.create({
    baseURL: `http://localhost:8081/`,
    withCredentials: true
  })

And I suppose this allows cookies -Which I am sure it shows in the browser before you ask- to be sent with the requests.

The problem occurs in the back-end, when this error shows in the log:

Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

I tried this:

app.use(cors({
  //origin : to be set later
  credentials: true,
}))

and this instead:

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  res.header("Access-Control-Allow-Credentials", true);
  next();
});

but neither seems to work.

EDIT - Here is the answer for future visitors

With the help of the participants in comments, I found out I had to set the origin value:

app.use(cors({
  origin : "http://localhost:8080",
  credentials: true,
}))


Solution 1:[1]

I know it's too late for OP, but for those who keep comming here - what helped me is:

app.use(cors({
  preflightContinue: true,
  credentials: true,
}));

source: https://expressjs.com/en/resources/middleware/cors.html

Solution 2:[2]

I got this problem and I fixed it by setting the header "Access-Control-Allow-Origin" to the URL of my frontend instead of "*"

More details: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 blocus