# `ShopifexWeb.LiveSession`
[🔗](https://github.com/ericdude4/shopifex/blob/main/lib/shopifex_web/live_session.ex#L1)

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"`).

# `on_mount`

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`

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
```

# `put_shop_in_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.

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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
