Skip to content

Instantly share code, notes, and snippets.

@Ninigi
Last active October 8, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ninigi/a2d916d6e9489e2c4c479ab6ac9da473 to your computer and use it in GitHub Desktop.
Save Ninigi/a2d916d6e9489e2c4c479ab6ac9da473 to your computer and use it in GitHub Desktop.
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