ShopifexWeb.LiveSession (Shopifex v3.0.0)

Copy Markdown View Source

Helpers and hooks for integrating LiveView with Shopifex.

Note that LiveView session keys are "shop_url" and "session_token" (in Shopifex 2.x the key was "current_shop").

Summary

Functions

LiveView on_mount hooks for Shopify apps.

Get options that should be passed to live_session.

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.

Functions

on_mount(atom, params, session, socket)

LiveView on_mount hooks for Shopify apps.

:assign_shop_to_socket (default)

Assigns current_shop and session_token from session to socket assigns. Use with the standard shopifex_live_session macro.

:embedded

Simplified hook for embedded Shopify apps where third-party cookies are blocked. Reads the shop from the session (set during the HTTP request by put_shop_in_session/1) and does NOT require tokens in URLs or LiveSocket connect params. Redirects to /auth (respecting path_prefix) if no shop is found.

How it works

  1. HTTP request arrives with id_token from App Bridge
  2. ManagedInstall plug validates the token and builds a Shopifex session
  3. put_shop_in_session/1 serializes the shop's URL (not the struct) into the LiveView session
  4. This hook reloads the shop from the session's shop_url server-side — no tokens needed

Within a live_session, LiveView preserves the session across navigations. Full page loads get a fresh id_token from App Bridge automatically.

Usage

live_session :my_app,
  on_mount: [{ShopifexWeb.LiveSession, :embedded}],
  session: {ShopifexWeb.LiveSession, :put_shop_in_session, []}

opts(custom_opts \\ [])

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:

beacon_live_admin ShopifexWeb.LiveSession.opts(...beacon_opts) do
  ...
end

put_shop_in_session(conn)

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.

Only the shop's URL is serialized (not the shop struct) — the LiveView session is signed but not encrypted, so it is readable client-side; storing the struct would leak access_token / refresh_token. on_mount reloads the shop from the URL server-side.