Last active
October 8, 2018 04:40
-
-
Save Ninigi/a2d916d6e9489e2c4c479ab6ac9da473 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Useless.Plugs.EmbeddedAppPlug do | |
@moduledoc """ | |
Loads the shop from database, puts a private shop field in conn and sets x-frame-options. | |
Will halt if no shop found | |
""" | |
alias Plug.Conn | |
alias Useless.ShopifyApp | |
@doc false | |
def init(opts \\ %{}), do: Enum.into(opts, %{}) | |
@doc false | |
def call(%{params: %{"shop" => shopify_domain}} = conn, _opts) do | |
shop = ShopifyApp.find_shop_by(%{shopify_domain: shopify_domain}) | |
allow_shop_or_halt(conn, shop) | |
end | |
def call(conn, _opts), do: conn | |
defp allow_shop_or_halt(conn, nil), do: Conn.halt(conn) | |
defp allow_shop_or_halt(conn, shop) do | |
conn | |
|> Conn.put_private(:shop, shop) | |
|> Conn.put_resp_header("x-frame-options", "ALLOW-FROM https://#{shop.shopify_domain}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment