Login User With LiveView

The initial phx.gen.auth generates the complete user authentication using Phoenix regular Views. Nothing LiveView about it. I am trying to register the user using a LiveView. Here is the “save” event of the LiveView

def handle_event("save", %{"user" => user_params}, socket) do
    case Accounts.register_user(user_params) do
      {:ok, user} ->
        {:ok, _} =
          Accounts.deliver_user_confirmation_instructions(
            user,
            &Routes.user_confirmation_url(socket, :edit, &1)
          )
        socket
        |> put_flash(
          :info,
          "You are registered and a confirmation email is sent. Check your email."
        )
        |> UserAuth.log_in_user(socket, user)
    end
  end

I am getting an weird error that UserToken.user_id in insert does not match type :id. However, the same code is working with a regular view. Any idea what mistake I am committing?
The UserAuth module is same as what is generated.

You cannot use liveview for this because You don’t have access to session.

In fact, You can with some hack… but it implies going to a non liveview controller to set the session. and return back to liveview.

There is a new live_session, that might help simplify auth a bit.

1 Like

@kokolegorille is right, it is all about the session. Bruce at Groxio did a great video about it here Liveview Auth

2 Likes