Skip to content

Commit d24ef51

Browse files
Baradoyhez
authored andcommitted
Use AssignScope for live_session validation
1 parent fef8d05 commit d24ef51

11 files changed

Lines changed: 58 additions & 24 deletions

File tree

assets/js/shop_admin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {ShopifyToastHook} from "./hooks"
99

1010
let Hooks = { ShopifyToastHook }
1111
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
12-
let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}, bindingPrefix: "data-phx-"})
12+
let liveSocket = new LiveSocket("/shop_admin_live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}, bindingPrefix: "data-phx-"})
1313

1414
// Show progress bar on live navigation and form submits
1515
window.addEventListener("phx:page-loading-start", _info => shopify.loading(true))
@@ -22,7 +22,7 @@ window.addEventListener("phx:page-loading-stop", info => {
2222
correctly patching within a live_session. This re-emits a navigation event
2323
that AppBridge will detect.
2424
*/
25-
destination = new URL(info.detail.to).pathname
25+
const destination = new URL(info.detail.to).pathname
2626
if (info.detail.kind == "initial" && destination != window.location.pathname) {
2727
history.pushState(null, '', destination);
2828
} else {

lib/shopify_app_web/components/shop_admin_components.ex

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ defmodule ShopifyAppWeb.ShopAdminComponents do
1212
~H"""
1313
<div>
1414
<ul>
15-
<li><.link patch={~p"/shop_admin/shopify_app/"}>Home</.link></li>
16-
<li><.link patch={~p"/shop_admin/shopify_app/settings"}>Settings</.link></li>
17-
<li><.link patch={~p"/shop_admin/shopify_app/more"}>More Settings</.link></li>
15+
<%!--
16+
# if we want to navigate back to react land, we need to remount with the HMAC
17+
See https://shopify.dev/docs/api/app-bridge-library/reference/navigation
18+
<li><a href="shopify:admin/apps/my-shopify-app-mount/shop_admin" target="_top">Home</a></li>
19+
--%>
20+
<li><.link patch={~p"/live_shop_admin/"}>Home</.link></li>
21+
<li><.link patch={~p"/live_shop_admin/show"}>Show</.link></li>
22+
<li><.link patch={~p"/live_shop_admin/settings"}>Settings</.link></li>
23+
<li><.link patch={~p"/live_shop_admin/more"}>More Settings</.link></li>
1824
</ul>
1925
</div>
2026
"""

lib/shopify_app_web/endpoint.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ defmodule ShopifyAppWeb.Endpoint do
88
store: :cookie,
99
key: "_shopify_app_key",
1010
signing_salt: "ijq1g2Yt",
11-
same_site: "None",
12-
secure: true
11+
same_site: "Lax"
1312
]
1413

1514
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
1615

16+
# For the ShopAdmin, we do not need anything in the session outside of what we put there from the conn.
17+
# by not defining session storage, we do not need to worry about cookies and how to access them.
18+
# The session info will be populated AssignScope and store in the DOM on `data-phx-session`
19+
socket "/shop_admin_live", Phoenix.LiveView.Socket, websocket: [connect_info: []]
20+
1721
# Serve at "/" the static files from "priv/static" directory.
1822
#
1923
# You should set gzip to true if you are running phx.digest

lib/shopify_app_web/live/shop_admin/index.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ShopifyAppWeb.ShopAdminLive.Index do
33

44
@impl true
55
def mount(_params, _session, socket) do
6-
{:ok, socket}
6+
{:ok, assign(socket, :connected, connected?(socket))}
77
end
88

99
@impl true
@@ -15,6 +15,10 @@ defmodule ShopifyAppWeb.ShopAdminLive.Index do
1515
assign(socket, :page_title, "Home")
1616
end
1717

18+
defp apply_action(socket, other, _params) do
19+
assign(socket, :page_title, "Home - #{other}")
20+
end
21+
1822
@impl true
1923
def handle_event("action", %{"title" => title}, socket) do
2024
{:noreply,

lib/shopify_app_web/live/shop_admin/index.html.heex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
</ui-title-bar>
99

1010
<header>
11-
Example <strong>Home</strong> LiveView
11+
Example <strong><%= @page_title %></strong> LiveView
1212
</header>
13+
Conected <%= @connected %>
1314

1415
<.navigation_links />

lib/shopify_app_web/live/shop_admin/layouts/root.html.heex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
</script>
1818

1919
<ui-nav-menu>
20-
<a href="/shop_admin/shopify_app/" rel="home">Root</a>
21-
<a href="/shop_admin/shopify_app/">Home</a>
22-
<a href="/shop_admin/shopify_app/settings">Settings</a>
23-
<a href="/shop_admin/shopify_app/more">More Settings</a>
20+
<a href="/live_shop_admin/" rel="home">Root</a>
21+
<a href="/live_shop_admin/">Home</a>
22+
<a href="/live_shop_admin/show">Show</a>
23+
<a href="/live_shop_admin/settings">Settings</a>
24+
<a href="/live_shop_admin/more">More Settings</a>
2425
</ui-nav-menu>
2526
</head>
2627
<body class="bg-white antialiased">

lib/shopify_app_web/live/shop_admin/settings.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ShopifyAppWeb.ShopAdminLive.Settings do
33

44
@impl true
55
def mount(_params, _session, socket) do
6-
{:ok, socket}
6+
{:ok, assign(socket, :connected, connected?(socket))}
77
end
88

99
@impl true
@@ -18,4 +18,12 @@ defmodule ShopifyAppWeb.ShopAdminLive.Settings do
1818
defp apply_action(socket, :more_settings, _params) do
1919
assign(socket, :page_title, "More Settings")
2020
end
21+
22+
@impl true
23+
def handle_event("action", %{"title" => title}, socket) do
24+
{:noreply,
25+
socket
26+
|> assign(:page_title, "Settings - " <> title)
27+
|> put_flash(:info, "A toast message from the flash!")}
28+
end
2129
end
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<ui-title-bar title={@page_title}>
2-
<button onclick="console.log('Secondary action')">Secondary action</button>
2+
<.button data-phx-click="action" data-phx-value-title="Secondary">
3+
Secondary action
4+
</.button>
35
</ui-title-bar>
46

57
<header>
6-
Example <strong>Settings</strong> LiveView
8+
Example <strong><%= @page_title %></strong> LiveView
79
</header>
10+
Conected <%= @connected %> <%= @live_action %> <%= @scope.shop.domain %>
811

912
<.navigation_links />

lib/shopify_app_web/router.ex

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule ShopifyAppWeb.Router do
1515
end
1616

1717
pipeline :shop_admin do
18-
plug ShopifyAPI.Plugs.AdminAuthenticator, shopify_router_mount: "/shop"
18+
plug ShopifyAPI.Plugs.AdminAuthenticator, app_name: "shopify_app"
1919
plug ShopifyAPI.Plugs.PutShopifyContentHeaders
2020
end
2121

@@ -42,14 +42,17 @@ defmodule ShopifyAppWeb.Router do
4242
end
4343
end
4444

45-
live_session :shop_admin,
45+
live_session :live_shop_admin,
4646
layout: {ShopifyAppWeb.ShopAdminLive.Layouts, :app},
47-
root_layout: {ShopifyAppWeb.ShopAdminLive.Layouts, :root} do
48-
scope "/shop_admin/:app", ShopifyAppWeb do
47+
root_layout: {ShopifyAppWeb.ShopAdminLive.Layouts, :root},
48+
on_mount: [ShopifyAppWeb.ShopAdminLive.AssignScope],
49+
session: {ShopifyAppWeb.ShopAdminLive.AssignScope, :build_session, []} do
50+
scope "/live_shop_admin", ShopifyAppWeb do
4951
pipe_through :browser
5052
pipe_through :shop_admin
5153

5254
live "/", ShopAdminLive.Index, :live
55+
live "/show", ShopAdminLive.Index, :show
5356
live "/settings", ShopAdminLive.Settings, :settings
5457
live "/more", ShopAdminLive.Settings, :more_settings
5558
end

test/shopify_app_web/live/shop_admin/index_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ defmodule ShopifyAppWeb.ShopAdminLive.IndexTest do
55
import ShopifyApp.ShopTestSetup
66

77
describe "ShopAdminLive.Index render" do
8-
setup [:shop]
8+
setup [:app_auth_token_shop]
99

10-
test "disconnected and connected mount", %{conn: conn, shopifyapi_shop: shop} do
10+
test "disconnected and connected mount", %{conn: conn, shopifyapi_shop: shop, app: app} do
1111
conn =
1212
conn
1313
|> assign(:shop, shop)
14-
|> get("/shop_admin/shopify_shop")
14+
|> assign(:app, app)
15+
|> get("/live_shop_admin")
1516

1617
assert html_response(conn, 200) =~ "Example <strong>Home</strong> LiveView"
1718

0 commit comments

Comments
 (0)