Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios - cross domain cookies

I have two applications (backend and frontend) running on local computer (frontend - Vue app on port 8080, backend - Scala app on port 9000)

I'm trying to send request to backend using Axios with cookie, but it works only, if both are running on the same domain:

It's working (cookie is present in API request):

backend address: http://127.0.0.1:9000/api/systems

frontend (in browser): http://127.0.0.1:8080

It doesn't work (request doesn't contain cookie):

backend address: http://127.0.0.1:9000/api/systems

frontend (in browser): http://localhost:8080

My frontend snippet with request:

const BASE_URL = appConfig.API_URL + '/api';
axios.defaults.withCredentials = true;
axios.get(BASE_URL + '/systems');

I think CORS are configured correctly:

play.filters.cors {
  allowedOrigins = ["http://localhost:8080", "http://127.0.0.1:8080"]
  supportsCredentials = true
  allowedHttpMethods = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"]
}
like image 402
Pawel Avatar asked Dec 13 '25 20:12

Pawel


1 Answers

try this on the backend:

const allowedOrigins = ['localhost',"http://localhost:8080", "http://127.0.0.1:8080","http://localhost:8080/"];
var corsOptions = {
  origin: function (origin, callback) {
    if (allowedOrigins.indexOf(origin) !== -1) {
      callback(null, true)
    } else {
      callback(new Error('Not allowed by CORS'))
      console.log("------")
      console.log("origin",origin)
    }
  },
  credentials: true,
}
like image 105
Govind S Kåtýurä Avatar answered Dec 16 '25 10:12

Govind S Kåtýurä



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!