phoenix-liveview-auth

Installation
SKILL.md

Phoenix LiveView Authentication

RULES — Follow these with no exceptions

  1. Always use on_mount callbacks for LiveView auth — never check auth in mount/3 directly; on_mount runs before mount and centralizes auth logic
  2. Use mount_current_scope/2 to extract scope from session — never access session tokens manually or parse session data in LiveViews
  3. Handle both :cont and :halt returns from on_mount:halt must redirect with a flash message, never silently drop the connection
  4. Resolve Controller/LiveView name clashes the way the 1.8 generator doesUserAuth contains both conn plugs and on_mount hooks. Import Phoenix.Controller normally (the plugs need redirect/2), and fully qualify the LiveView calls inside on_mount hooks: Phoenix.LiveView.redirect(socket, to: ...) and Phoenix.LiveView.put_flash(socket, :error, ...). Excluding the Controller imports breaks the plug half of the module.
  5. Guard the nil scope, not the assign lookupmount_current_scope always assigns :current_scope, so @current_scope is safe; the hazard is calling .user on a nil scope. Write @current_scope && @current_scope.user. Bracket access assigns[:current_scope] is only needed when the assign may be entirely absent (e.g. layouts shared with non-auth live_sessions).
  6. Test auth redirects by asserting {:error, {:redirect, %{to: path}}} — don't test auth by checking rendered content; verify the redirect tuple from live/2
  7. Define on_mount hooks once, reference via live_session in router — never duplicate auth logic across LiveView modules

on_mount Authentication Pattern

The standard pattern for LiveView authentication. Define once, use everywhere via live_session.

Installs
5
GitHub Stars
147
First Seen
Apr 21, 2026
phoenix-liveview-auth — j-morgan6/elixir-phoenix-guide