Skip to content

Commit d41ffd4

Browse files
committed
Add CORS plug limited to the specific path
It is not entirely clear why the plug does not work in a pipeline, but it does not. Likely this is related to how the macro for liveviews works
1 parent bd89a49 commit d41ffd4

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/shopify_app/plugs/cors.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule ShopifyApp.Plug.CORS do
2+
@moduledoc """
3+
Allows CORS for the specific path.
4+
5+
# Example
6+
7+
plug ShopifyApp.Plug.CORS, path: "shop_admin"
8+
"""
9+
10+
@cors CORSPlug.init(origin: ["*"])
11+
12+
def init(opts), do: Keyword.fetch!(opts, :path)
13+
14+
def call(%Plug.Conn{path_info: [path | _]} = conn, path), do: CORSPlug.call(conn, @cors)
15+
def call(conn, _opts), do: conn
16+
end

lib/shopify_app_web/endpoint.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ defmodule ShopifyAppWeb.Endpoint do
1818
# The session info will be populated AssignScope and store in the DOM on `data-phx-session`
1919
socket "/shop_admin_live", Phoenix.LiveView.Socket, websocket: [connect_info: []]
2020

21+
# Add CORS specifically for the "/shop_admin" route for embeding LiveView in react.
22+
plug ShopifyApp.Plug.CORS, path: "shop_admin"
23+
2124
# Serve at "/" the static files from "priv/static" directory.
2225
#
2326
# You should set gzip to true if you are running phx.digest

lib/shopify_app_web/router.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ defmodule ShopifyAppWeb.Router do
2121

2222
pipeline :embeded_shop_admin do
2323
plug :accepts, ["html"]
24-
plug CORSPlug, origin: ["*"]
2524
plug ShopifyAPI.Plugs.AuthShopSessionToken
2625
plug ShopifyAPI.Plugs.PutShopifyContentHeaders
2726
end

0 commit comments

Comments
 (0)