Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The on_mount override on sign_in_route doesn't get the current user set #367

Closed
sevenseacat opened this issue Feb 1, 2024 · 3 comments · Fixed by #369
Closed

The on_mount override on sign_in_route doesn't get the current user set #367

sevenseacat opened this issue Feb 1, 2024 · 3 comments · Fixed by #369

Comments

@sevenseacat
Copy link
Member

The guide for Ash authentication w/ LiveView mentions:

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}])

I've added this in my app:

sign_in_route register_path: "/register",
                  reset_path: "/reset",
                  on_mount: [{MyAppWeb.LiveUserAuth, :live_no_user}],
                  overrides: [MyAppWeb.AuthOverrides, AshAuthentication.Phoenix.Overrides.Default]

With the MyAppWeb.LiveUserAuth module -

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.

[(my_app 0.1.0) lib/my_app_web/live_user_auth.ex:30: MyAppWeb.LiveUserAuth.on_mount/4]
socket.assigns #=> %{flash: %{}, __changed__: %{}, live_action: :sign_in}

So a user can still visit the sign-in page, even when already signed in.

@jimsynz
Copy link
Collaborator

jimsynz commented Feb 1, 2024

Do you have plug :load_from_session in your :browser pipeline?

@sevenseacat
Copy link
Member Author

Yep! Just double checked

@jimsynz
Copy link
Collaborator

jimsynz commented Feb 2, 2024

Okay that's super weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants