Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

[Sentry APP-5B] Ensure form errors can be rendered on /settings (#2278)

Uku Taht65f0ce3061a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6ebf841406..3edb24e91f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -53,6 +53,7 @@ All notable changes to this project will be documented in this file.
- Timezone offset labels now update with time changes
- Render 404 if shared link auth cannot be verified [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225)
- Restore compatibility with older format of shared links [plausible/analytics#2225](https://github.com/plausible/analytics/pull/2225)
+- Ensure settings page can be rendered after a form error [plausible/analytics#2278](https://github.com/plausible/analytics/pull/2278)
### Changed
- Cache the tracking script for 24 hours
diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex
index c6e55b8af6..a4cffe1a50 100644
--- a/lib/plausible_web/controllers/auth_controller.ex
+++ b/lib/plausible_web/controllers/auth_controller.ex
@@ -437,20 +437,8 @@ defmodule PlausibleWeb.AuthController do
end
def user_settings(conn, _params) do
- user = conn.assigns[:current_user]
- changeset = Auth.User.changeset(user)
-
- {usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user)
-
- render(conn, "user_settings.html",
- user: user |> Repo.preload(:api_keys),
- changeset: changeset,
- subscription: user.subscription,
- invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
- theme: user.theme || "system",
- usage_pageviews: usage_pageviews,
- usage_custom_events: usage_custom_events
- )
+ changeset = Auth.User.changeset(conn.assigns[:current_user])
+ render_settings(conn, changeset)
end
def save_settings(conn, %{"user" => user_params}) do
@@ -463,13 +451,25 @@ defmodule PlausibleWeb.AuthController do
|> redirect(to: Routes.auth_path(conn, :user_settings))
{:error, changeset} ->
- render(conn, "user_settings.html",
- changeset: changeset,
- subscription: conn.assigns[:current_user].subscription
- )
+ render_settings(conn, changeset)
end
end
+ defp render_settings(conn, changeset) do
+ user = conn.assigns[:current_user]
+ {usage_pageviews, usage_custom_events} = Plausible.Billing.usage_breakdown(user)
+
+ render(conn, "user_settings.html",
+ user: user |> Repo.preload(:api_keys),
+ changeset: changeset,
+ subscription: user.subscription,
+ invoices: Plausible.Billing.paddle_api().get_invoices(user.subscription),
+ theme: user.theme || "system",
+ usage_pageviews: usage_pageviews,
+ usage_custom_events: usage_custom_events
+ )
+ end
+
def new_api_key(conn, _params) do
key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64)
changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, %{key: key})
diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs
index 2aaa0c482a..dc3d140885 100644
--- a/test/plausible_web/controllers/auth_controller_test.exs
+++ b/test/plausible_web/controllers/auth_controller_test.exs
@@ -584,6 +584,12 @@ defmodule PlausibleWeb.AuthControllerTest do
assert redirected_to(conn, 302) == "/settings"
end
+
+ test "renders form with error if form validations fail", %{conn: conn, user: user} do
+ conn = put(conn, "/settings", %{"user" => %{"name" => ""}})
+
+ assert html_response(conn, 200) =~ "can't be blank"
+ end
end
describe "DELETE /me" do