You can also use this to prevent users from visiting the auto generated sign_in route:
sign_in_route(on_mount: [{MyAppWeb.LiveUserAuth, :live_no_user}])
defmodule MyAppWeb.LiveUserAuth do
@moduledoc """
Helpers for authenticating users in LiveViews.
"""
import Phoenix.Component
use MyAppWeb, :verified_routes
# the rest of the functions
def on_mount(:live_no_user, _params, _session, socket) do
dbg(socket.assigns)
if socket.assigns[:current_user] do
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
else
{:cont, assign(socket, :current_user, nil)}
end
end
end
But current_user is never set in the socket.assigns, whether a user is logged in or not.
The guide for Ash authentication w/ LiveView mentions:
I've added this in my app:
With the
MyAppWeb.LiveUserAuth
module -But
current_user
is never set in thesocket.assigns
, whether a user is logged in or not.So a user can still visit the sign-in page, even when already signed in.