phoenix-liveview-auth
Installation
SKILL.md
Phoenix LiveView Authentication
RULES — Follow these with no exceptions
- Always use
on_mountcallbacks for LiveView auth — never check auth inmount/3directly;on_mountruns before mount and centralizes auth logic - Use
mount_current_scope/2to extract scope from session — never access session tokens manually or parse session data in LiveViews - Handle both
:contand:haltreturns fromon_mount—:haltmust redirect with a flash message, never silently drop the connection - Resolve Controller/LiveView name clashes the way the 1.8 generator does —
UserAuthcontains both conn plugs and on_mount hooks. ImportPhoenix.Controllernormally (the plugs needredirect/2), and fully qualify the LiveView calls inside on_mount hooks:Phoenix.LiveView.redirect(socket, to: ...)andPhoenix.LiveView.put_flash(socket, :error, ...). Excluding the Controller imports breaks the plug half of the module. - Guard the nil scope, not the assign lookup —
mount_current_scopealways assigns:current_scope, so@current_scopeis safe; the hazard is calling.useron a nil scope. Write@current_scope && @current_scope.user. Bracket accessassigns[:current_scope]is only needed when the assign may be entirely absent (e.g. layouts shared with non-auth live_sessions). - Test auth redirects by asserting
{:error, {:redirect, %{to: path}}}— don't test auth by checking rendered content; verify the redirect tuple fromlive/2 - Define
on_mounthooks once, reference vialive_sessionin 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.