From e0c6da10f8da8d6176ba57004a8c06682f402593 Mon Sep 17 00:00:00 2001 From: Dan Schultzer <1254724+danschultzer@users.noreply.github.com> Date: Mon, 27 Mar 2023 15:54:30 -0700 Subject: [PATCH] Log error when user can't be created --- CHANGELOG.md | 6 ++++++ .../phoenix/controllers/authorization_controller.ex | 9 ++++++++- .../controllers/authorization_controller_test.exs | 12 +++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8e3b5b..c127baf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v0.4.17 (TBA) + +### Enhancements + +* [`PowAssent.Phoenix.AuthorizationController`] Now logs error for when user can't be created + ## v0.4.16 (2023-03-21) Now uses Phoenix 1.7 components templating and requires Pow 1.0.29. diff --git a/lib/pow_assent/phoenix/controllers/authorization_controller.ex b/lib/pow_assent/phoenix/controllers/authorization_controller.ex index c8da09b..0fe8935 100644 --- a/lib/pow_assent/phoenix/controllers/authorization_controller.ex +++ b/lib/pow_assent/phoenix/controllers/authorization_controller.ex @@ -83,7 +83,14 @@ defmodule PowAssent.Phoenix.AuthorizationController do |> redirect(to: routes(conn).path_for(conn, RegistrationController, :add_user_id, [provider])) end) end - def respond_callback({:error, conn}) do + def respond_callback({:error, %{private: %{pow_assent_callback_state: {:error, :create_user}, pow_assent_registration: false}} = conn}) do + conn + |> put_flash(:error, extension_messages(conn).could_not_sign_in(conn)) + |> redirect(to: routes(conn).session_path(conn, :new)) + end + def respond_callback({:error, %{private: %{pow_assent_callback_state: {:error, :create_user}, pow_assent_callback_error: changeset}} = conn}) do + Logger.error("Unexpected error inserting user: #{inspect changeset}") + conn |> put_flash(:error, extension_messages(conn).could_not_sign_in(conn)) |> redirect(to: routes(conn).session_path(conn, :new)) diff --git a/test/pow_assent/phoenix/controllers/authorization_controller_test.exs b/test/pow_assent/phoenix/controllers/authorization_controller_test.exs index 47ef2e6..f0b7a93 100644 --- a/test/pow_assent/phoenix/controllers/authorization_controller_test.exs +++ b/test/pow_assent/phoenix/controllers/authorization_controller_test.exs @@ -193,12 +193,14 @@ defmodule PowAssent.Phoenix.AuthorizationControllerTest do test "when identity doesn't exist and missing params", %{conn: conn} do set_oauth2_test_endpoints(user: %{sub: "new_user", name: ""}) - conn = get conn, Routes.pow_assent_authorization_path(conn, :callback, @provider, @callback_params) + assert capture_log(fn -> + conn = get conn, Routes.pow_assent_authorization_path(conn, :callback, @provider, @callback_params) - assert redirected_to(conn) == Routes.pow_session_path(conn, :new) - assert get_flash(conn, :error) == "Something went wrong, and you couldn't be signed in. Please try again." - refute conn.resp_cookies["pow_assent_auth_session"] - refute get_pow_assent_session(conn, :session_params) + assert redirected_to(conn) == Routes.pow_session_path(conn, :new) + assert get_flash(conn, :error) == "Something went wrong, and you couldn't be signed in. Please try again." + refute conn.resp_cookies["pow_assent_auth_session"] + refute get_pow_assent_session(conn, :session_params) + end) =~ "Unexpected error inserting user: #Ecto.Changeset