AuditTrail.LiveViewAudit (audit_trail v0.1.0)

Copy Markdown

Declarative audit logging for LiveView handle_event/3, mirroring AuditTrail.ControllerAudit's write/read config style for controllers.

Usage

defmodule MyAppWeb.ItemLive do
  use MyAppWeb, :live_view

  on_mount {AuditTrail.LiveViewAudit,
    resource: "item",
    write: ["approve", "decline"],
    read:  ["view_details"]}

  def handle_event("approve", %{"id" => id}, socket) do
    ...
  end
end

Or attach directly if you don't use on_mount (e.g. inside mount/3):

def mount(_params, _session, socket) do
  {:ok, AuditTrail.LiveViewAudit.attach(socket, resource: "item", write: ["approve"])}
end

How this differs from ControllerAudit

Phoenix.LiveView.attach_hook/4 intercepts handle_event/3 before your own callback runs — there's no LiveView equivalent of register_before_send to inspect an outcome once your callback returns. So unlike ControllerAudit, there's no HTTP-status-like success/failure distinction: every configured event is logged the moment it's dispatched, regardless of whether your handle_event/3 clause then succeeds, raises, or no-ops. write:/read: behave identically today — the split exists so the config shape matches ControllerAudit and so operation is tagged for filtering.

resource_id is picked up from the event params' "id"/"uuid" key (or whatever id_param: names), same convention as ControllerAudit. Sensitive keys (password, token, etc.) are stripped the same way too.

Summary

Functions

Attach the audit hook to a socket directly, e.g. from mount/3.

Use as an on_mount hook: on_mount {AuditTrail.LiveViewAudit, opts}.

Functions

attach(socket, opts)

Attach the audit hook to a socket directly, e.g. from mount/3.

on_mount(opts, params, session, socket)

Use as an on_mount hook: on_mount {AuditTrail.LiveViewAudit, opts}.