Skip to content

Commit

Permalink
phx.gen.auth: don't generate on_mount functions when not needed (#6049)
Browse files Browse the repository at this point in the history
Closes #6045.
  • Loading branch information
SteffenDE authored Jan 26, 2025
1 parent cc39fb5 commit f16c7e4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions priv/templates/phx.gen.auth/auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule <%= inspect auth_module %> do
end
end

@doc """
<%= if live? do %>@doc """
Handles mounting and authenticating the current_<%= schema.singular %> in LiveViews.
## `on_mount` arguments
Expand Down Expand Up @@ -182,7 +182,7 @@ defmodule <%= inspect auth_module %> do
end)
end

@doc """
<% end %>@doc """
Used for routes that require the <%= schema.singular %> to not be authenticated.
"""
def redirect_if_<%= schema.singular %>_is_authenticated(conn, _opts) do
Expand Down
8 changes: 4 additions & 4 deletions priv/templates/phx.gen.auth/auth_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule <%= inspect auth_module %>Test do
use <%= inspect context.web_module %>.ConnCase<%= test_case_options %>

alias Phoenix.LiveView
alias <%= inspect context.module %>
<%= if live? do %>alias Phoenix.LiveView
<% end %>alias <%= inspect context.module %>
alias <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Auth
import <%= inspect context.module %>Fixtures

Expand Down Expand Up @@ -117,7 +117,7 @@ defmodule <%= inspect auth_module %>Test do
end
end
describe "on_mount :mount_current_<%= schema.singular %>" do
<%= if live? do %>describe "on_mount :mount_current_<%= schema.singular %>" do
test "assigns current_<%= schema.singular %> based on a valid <%= schema.singular %>_token", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
<%= schema.singular %>_token = <%= inspect context.alias %>.generate_<%= schema.singular %>_session_token(<%= schema.singular %>)
session = conn |> put_session(:<%= schema.singular %>_token, <%= schema.singular %>_token) |> get_session()
Expand Down Expand Up @@ -212,7 +212,7 @@ defmodule <%= inspect auth_module %>Test do
end
end

describe "redirect_if_<%= schema.singular %>_is_authenticated/2" do
<% end %>describe "redirect_if_<%= schema.singular %>_is_authenticated/2" do
test "redirects if <%= schema.singular %> is authenticated", %{conn: conn, <%= schema.singular %>: <%= schema.singular %>} do
conn = conn |> assign(:current_<%= schema.singular %>, <%= schema.singular %>) |> <%= inspect schema.alias %>Auth.redirect_if_<%= schema.singular %>_is_authenticated([])
assert conn.halted
Expand Down

0 comments on commit f16c7e4

Please sign in to comment.