Keyfob.Live (Keyfob v0.1.0)

Copy Markdown View Source

LiveView glue for the desktop panel. Handles the create → subscribe → render → await-approval → navigate cycle so a login LiveView only has to render Keyfob.Components.panel/1 and forward one message.

Usage

# mount (only when connected, so the request isn't minted twice)
socket =
  if connected?(socket),
    do: Keyfob.Live.init_panel(socket, confirm_url: &confirm_url/1, meta: device_meta(socket)),
    else: assign(socket, keyfob: nil)

# in render
<Keyfob.Components.panel :if={@keyfob} qr_svg={@keyfob.qr_svg} state={@keyfob.state} />

# forward the PubSub message + refresh click
def handle_info({:keyfob, _t, _} = msg, socket),
  do: Keyfob.Live.handle_message(msg, socket, on_approved: &complete/2)

def handle_event("keyfob_refresh", _p, socket),
  do: {:noreply, Keyfob.Live.refresh(socket)}

on_approved.(socket, login_token) decides where to go — typically push_navigate(socket, to: completion_url(login_token)).

Summary

Functions

Marks the panel expired (e.g. from a client/server TTL timer).

Handles {:keyfob, token, payload} messages for the panel.

Mints a request and stores panel state under the :keyfob assign.

Re-mints the request (new QR), unsubscribing the old token first. Use from the panel's refresh button and, if you drive it, an expiry timer.

Functions

expire(socket)

Marks the panel expired (e.g. from a client/server TTL timer).

handle_message(arg, socket, opts \\ [])

Handles {:keyfob, token, payload} messages for the panel.

On {:approved, login_token} sets state :approved and calls opts[:on_approved].(socket, login_token) (which should navigate). On :denied sets state :denied. Messages for a stale token (after a refresh) are ignored.

init_panel(socket, opts)

Mints a request and stores panel state under the :keyfob assign.

Options:

  • :confirm_url(token -> url) building the URL encoded in the QR (your phone confirm page). Required.
  • :meta — device meta for the confirm screen. Default %{}.
  • :qr — options forwarded to Keyfob.QR.svg/2.
  • plus any Keyfob.create_request/1 option (:ttl_ms, :pubsub, :store).

Call only when the socket is connected.

refresh(socket)

Re-mints the request (new QR), unsubscribing the old token first. Use from the panel's refresh button and, if you drive it, an expiry timer.