aaronrenner / phx_gen_auth

An authentication system generator for Phoenix 1.5 applications.
774 stars 56 forks source link

MatchError at POST /users/register - no match of right hand side value: :ok #73

Closed owenstrevor closed 4 years ago

owenstrevor commented 4 years ago

Hello, I just setup UserNotifier.ex to work with Sendgrid and now I'm getting this error.

def create(conn, %{"user" => user_params}) do
    case Accounts.register_user(user_params) do
      {:ok, user} ->
        {:ok, _} =
          Accounts.deliver_user_confirmation_instructions(
            user,
            &Routes.user_confirmation_url(conn, :confirm, &1)
          )

        conn
        |> put_flash(:info, "User created successfully.")
        |> UserAuth.log_in_user(user)

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
end

The error is with the 4th line of the above, {:ok, _} =

User Notifier has been working fine for Password Reset emails.

IO.inspect Email.build()
    |> Email.put_from("myemail@myemail.com")
    |> Email.add_to(to)
    |> Email.put_subject(subject)
    |> Email.put_text(body)
    |> Mail.send()

The IO.inspect returns :ok

aaronrenner commented 4 years ago

Sorry to hear you're running into issues. It's tough to troubleshoot without seeing your UserNotifier module, so this feedback will just be general.

The idea is that you should be able replace the body of UserNotifier.deliver/2 with whatever you need to send the email and as long as it still returns {:ok, %{to: to, body: body}}, everything should continue to work. My hunch is Mail.send/1 is returning :ok instead of {:ok, %{to: to, body: body}}. The reason this works for password reset emails is because the UserPasswordResetController.create/2 does not pattern match on the result of Accounts.deliver_user_reset_password_instructions/2.

Hopefully this suggestion helps you fix the issue you're facing. If you still have issues, it might be helpful to post your UserNotifier module.

owenstrevor commented 4 years ago

Thank you Aaron for the thoughtful response. This provided me some help with finding the fix. The helpful folks on the Elixir slack also helped.

Just needed to chain this at the end of the deliver function

|> case do
      :ok -> {:ok, %{to: to, body: body}}
      :error -> IO.inspect
end