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

Keep email confirmation token on password update. #5951

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions priv/templates/phx.gen.auth/context_functions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,7 @@
|> <%= inspect schema.alias %>.password_changeset(attrs)
|> <%= inspect schema.alias %>.validate_current_password(password)

Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, changeset)
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Repo.transaction()
|> case do
{:ok, %{<%= schema.singular %>: <%= schema.singular %>}} -> {:ok, <%= schema.singular %>}
{:error, :<%= schema.singular %>, changeset, _} -> {:error, changeset}
end
update_password(<%= schema.singular %>, changeset)
end

## Session
Expand Down Expand Up @@ -333,9 +326,15 @@

"""
def reset_<%= schema.singular %>_password(<%= schema.singular %>, attrs) do
update_password(<%= schema.singular %>, <%= inspect schema.alias %>.password_changeset(<%= schema.singular %>, attrs))
end

defp update_password(<%= schema.singular %>, changeset) do
tokens_query = <%= inspect schema.alias %>Token.by_<%= schema.singular %>_except_contexts_query(<%= schema.singular %>, ["confirm"])

Ecto.Multi.new()
|> Ecto.Multi.update(:<%= schema.singular %>, <%= inspect schema.alias %>.password_changeset(<%= schema.singular %>, attrs))
|> Ecto.Multi.delete_all(:tokens, <%= inspect schema.alias %>Token.by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all))
|> Ecto.Multi.update(:<%= schema.singular %>, changeset)
|> Ecto.Multi.delete_all(:tokens, tokens_query)
|> Repo.transaction()
|> case do
{:ok, %{<%= schema.singular %>: <%= schema.singular %>}} -> {:ok, <%= schema.singular %>}
Expand Down
14 changes: 12 additions & 2 deletions priv/templates/phx.gen.auth/schema_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,22 @@ defmodule <%= inspect schema.module %>Token do
end

@doc """
Gets all tokens for the given <%= schema.singular %> for the given contexts.
Gets all tokens for the given <%= schema.singular %>.
"""
def by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, :all) do
def by_<%= schema.singular %>_query(<%= schema.singular %>) do
from t in <%= inspect schema.alias %>Token, where: t.<%= schema.singular %>_id == ^<%= schema.singular %>.id
end

@doc """
Gets all tokens for the given <%= schema.singular %> except the given contexts.
"""
def by_<%= schema.singular %>_except_contexts_query(<%= schema.singular %>, [_ | _] = contexts) do
from t in <%= inspect schema.alias %>Token, where: t.<%= schema.singular %>_id == ^<%= schema.singular %>.id and t.context not in ^contexts
end

@doc """
Gets all tokens for the given <%= schema.singular %> for the given contexts.
"""
def by_<%= schema.singular %>_and_contexts_query(<%= schema.singular %>, [_ | _] = contexts) do
from t in <%= inspect schema.alias %>Token, where: t.<%= schema.singular %>_id == ^<%= schema.singular %>.id and t.context in ^contexts
end
Expand Down
1 change: 1 addition & 0 deletions priv/templates/phx.gen.auth/settings_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web
type="password"
label="Confirm new password"
autocomplete="new-password"
required
/>
<.input
field={@password_form[:current_password]}
Expand Down
13 changes: 9 additions & 4 deletions priv/templates/phx.gen.auth/test_cases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,17 @@
assert <%= inspect context.alias %>.get_<%= schema.singular %>_by_email_and_password(<%= schema.singular %>.email, "new valid password")
end

test "deletes all tokens for the given <%= schema.singular %>", %{<%= schema.singular %>: <%= schema.singular %>} do
test "deletes all tokens except confirmation for the given <%= schema.singular %>", %{<%= schema.singular %>: <%= schema.singular %>} do
_ = <%= inspect context.alias %>.generate_<%= schema.singular %>_session_token(<%= schema.singular %>)
_ = <%= inspect context.alias %>.deliver_<%= schema.singular %>_reset_password_instructions(<%= schema.singular %>, & &1)
_ = <%= inspect context.alias %>.deliver_<%= schema.singular %>_confirmation_instructions(<%= schema.singular %>, & &1)

{:ok, _} =
<%= inspect context.alias %>.update_<%= schema.singular %>_password(<%= schema.singular %>, valid_<%= schema.singular %>_password(), %{
password: "new valid password"
})

refute Repo.get_by(<%= inspect schema.alias %>Token, <%= schema.singular %>_id: <%= schema.singular %>.id)
assert [%<%= inspect schema.alias %>Token{context: "confirm"}] = Repo.all(<%= inspect schema.alias %>Token.by_<%= schema.singular %>_query(<%= schema.singular %>))
end
end

Expand Down Expand Up @@ -488,10 +490,13 @@
assert <%= inspect context.alias %>.get_<%= schema.singular %>_by_email_and_password(<%= schema.singular %>.email, "new valid password")
end

test "deletes all tokens for the given <%= schema.singular %>", %{<%= schema.singular %>: <%= schema.singular %>} do
test "deletes all tokens except confirmation for the given <%= schema.singular %>", %{<%= schema.singular %>: <%= schema.singular %>} do
_ = <%= inspect context.alias %>.generate_<%= schema.singular %>_session_token(<%= schema.singular %>)
_ = <%= inspect context.alias %>.deliver_<%= schema.singular %>_reset_password_instructions(<%= schema.singular %>, & &1)
_ = <%= inspect context.alias %>.deliver_<%= schema.singular %>_confirmation_instructions(<%= schema.singular %>, & &1)

{:ok, _} = <%= inspect context.alias %>.reset_<%= schema.singular %>_password(<%= schema.singular %>, %{password: "new valid password"})
refute Repo.get_by(<%= inspect schema.alias %>Token, <%= schema.singular %>_id: <%= schema.singular %>.id)
assert [%<%= inspect schema.alias %>Token{context: "confirm"}] = Repo.all(<%= inspect schema.alias %>Token.by_<%= schema.singular %>_query(<%= schema.singular %>))
end
end

Expand Down
Loading