cyu / rack-cors

Rack Middleware for handling Cross-Origin Resource Sharing (CORS), which makes cross-origin AJAX possible.
MIT License
3.26k stars 263 forks source link

Usage with Capacitor/Ionic #223

Closed wolak88 closed 3 years ago

wolak88 commented 3 years ago

I have a rails backend server that serves multiple domains with PWA white-labeled applications. We are using capacitor to have native iOS app in the Apple App Store. Everything works until we try to use websockets. I get cors error because origin is prefixed with capacitor:// instead of https://. My cors config looks like:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins "*"
    resource "*",
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head, :connect, :trace]
  end

  allow do
    origins "capacitor://domain.name"
    resource "*",
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head, :connect, :trace]
  end
end

Second allow here with explicit origin was added as supposed fix but I still get: Request origin not allowed: capacitor://domain.name

wolak88 commented 3 years ago

It turns out that I had specified HTTP or HTTPS origins in Rails config for ActionCable:

config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]