Packages
shopifex
2.4.0
2.4.0
2.3.0
2.2.2
2.2.1
2.2.0
2.1.20
2.1.19
2.1.18
2.1.17
2.1.16
2.1.15
2.1.14
2.1.13
2.1.12
2.1.11
2.1.10
2.1.9
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
1.1.1
1.1.0
1.0.1
1.0.0
0.6.2
0.6.1
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Phoenix boilerplate for Shopify Embedded App SDK
Current section
Files
Jump to
Current section
Files
lib/shopifex_web/live_session.ex
defmodule ShopifexWeb.LiveSession do
# To avoid making LiveView a dependency of the entire application, we'll
# silence this warning. At runtime, the LiveView module will be available
# if the application is using LiveView.
@compile {:no_warn_undefined, Phoenix.Component}
@doc """
Get options that should be passed to `live_session`.
This is useful for integrating with other tools that require a custom `live_session`,
like `beacon_live_admin`. For example:
```elixir
beacon_live_admin ShopifexWeb.LiveSession.opts(...beacon_opts) do
...
end
```
"""
def opts(custom_opts \\ []) do
on_mount = {__MODULE__, :assign_shop_to_socket}
session = {__MODULE__, :put_shop_in_session, []}
custom_opts
|> Keyword.update(:on_mount, on_mount, &([on_mount] ++ List.wrap(&1)))
|> Keyword.put(:session, session)
end
@doc """
Return a map of session values to include in the liveview session. This
will be merged with other session values and available in the on_mount.
"""
def put_shop_in_session(conn) do
session_token = Shopifex.Plug.session_token(conn)
current_shop = Shopifex.Plug.current_shop(conn)
%{"session_token" => session_token, "current_shop" => current_shop}
end
@doc """
Add the current_shop and session_token to assigns making them available
in live view templates.
"""
def on_mount(:assign_shop_to_socket, _params, session, socket) do
assigns = %{
current_shop: session["current_shop"],
session_token: session["session_token"]
}
{:cont, Phoenix.Component.assign(socket, assigns)}
end
end