Pow: Best approach for clearing a current email confirmation (update email)

Created on 2 Jun 2021  路  4Comments  路  Source: danschultzer/pow

Hey there!

Firstly, I just wanted to thank you for this wonderful lib, it's really made auth and related functionality a breeze!

We are using the EmailConfirmation extension with a custom controller flow, but want to add the ability for a user to undo a current email change.

The scenario might be:

  • A user wants to update their email, they submit their new email address
  • An email confirmation is sent, and the user sees a message indicating that they have a pending email confirmation
  • The user decides they no longer want to update their email, and click a link to cancel the email update, and revert things back to their original state

What would you suggest is the best way to do this?

TY!
Sebastian

All 4 comments

Thanks!

You just need to submit the same email as the account currently has to reset it: https://github.com/danschultzer/pow/blob/v1.0.24/lib/extensions/email_confirmation/ecto/schema.ex#L77-L78

The way you present it to the end-user is up to you. E.g. if you use a link then you could call Pow.Plug.update_user/2 in the controller:

case Pow.Plug.update_user(conn, %{"email" => conn.assigns.current_user.email}) do
  {:ok, _user, conn} ->
    # Redirect

  {:error, changeset, conn} ->
    # show error
end

The above assume that the user is already signed in using the current_user assign. You could also set up a specific context function for it that calls the changeset function in PowEmailConfirmation.Ecto.Schema.

Thanks @danschultzer makes sense!

@danschultzer One thing we're noticing is Pow.Plug.update_user requires a current_password is there a way to bypass this?

Oh right! In this case you could just set up a changeset function that uses the PowEmailConfirmation changeset directly. Could look like this (not tested):

# User schema

  def reset_email_confirmation_changeset(user) do
    attrs = %{email: user.email}

    user
    |> change()
    |> PowEmailConfirmation.Ecto.Schema.changeset(attrs, @pow_extension_config)
  end
# Users context

  def reset_email_confirmation(user) do
    user
    |> User.reset_email_confirmation_changeset()
    |> Repo.update()
  end
Was this page helpful?
0 / 5 - 0 ratings