VoileWeb.Live.AuthHooks (Voile v0.1.31)

Copy Markdown View Source

LiveView hooks for handling authentication and authorization.

These hooks automatically catch authorization errors and handle them gracefully by redirecting users with friendly flash messages instead of showing 500 errors.

Usage

Wrap your mount function with handle_mount_errors:

def mount(params, session, socket) do
  handle_mount_errors do
    authorize!(socket, "some.permission")
    # ... rest of mount logic
    {:ok, socket}
  end
end

Summary

Functions

Wraps mount logic to automatically catch and handle authorization errors.

Functions

handle_mount_errors(list)

(macro)

Wraps mount logic to automatically catch and handle authorization errors.

Examples

def mount(_params, _session, socket) do
  handle_mount_errors do
    authorize!(socket, "system.settings")
    {:ok, assign(socket, :data, load_data())}
  end
end