From e2fd8de68481a11bd560d8f82cff51a734d95833 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 12:39:31 -0500 Subject: [PATCH 01/37] Add Pow --- registrations/lib/registrations/users/user.ex | 10 ++++++++++ registrations/mix.exs | 1 + registrations/mix.lock | 1 + .../migrations/20240714173901_create_users.exs | 14 ++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 registrations/lib/registrations/users/user.ex create mode 100644 registrations/priv/repo/migrations/20240714173901_create_users.exs diff --git a/registrations/lib/registrations/users/user.ex b/registrations/lib/registrations/users/user.ex new file mode 100644 index 00000000..483e483d --- /dev/null +++ b/registrations/lib/registrations/users/user.ex @@ -0,0 +1,10 @@ +defmodule Registrations.Users.User do + use Ecto.Schema + use Pow.Ecto.Schema + + schema "users" do + pow_user_fields() + + timestamps() + end +end diff --git a/registrations/mix.exs b/registrations/mix.exs index 90ffc737..b8a18220 100644 --- a/registrations/mix.exs +++ b/registrations/mix.exs @@ -42,6 +42,7 @@ defmodule Registrations.Mixfile do {:phoenix_live_reload, "~> 1.0", only: :dev}, {:plug_cowboy, "~> 2.1"}, {:plug, "~> 1.7"}, + {:pow, "~> 1.0.28"}, {:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false}, {:hound, github: "backspace/hound", ref: "malgasm-plus-warning-fixes", only: :test}, {:ex_machina, "~> 2.7.0", only: :test}, diff --git a/registrations/mix.lock b/registrations/mix.lock index d9593f67..f004fae1 100644 --- a/registrations/mix.lock +++ b/registrations/mix.lock @@ -45,6 +45,7 @@ "plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"}, "plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"}, "postgrex": {:hex, :postgrex, "0.17.3", "c92cda8de2033a7585dae8c61b1d420a1a1322421df84da9a82a6764580c503d", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "946cf46935a4fdca7a81448be76ba3503cff082df42c6ec1ff16a4bdfbfb098d"}, + "pow": {:hex, :pow, "1.0.28", "b2cc32673e3fd138db6bd7e0656d826b287800d60f4139ed3c3fa6df926f1250", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0 and < 1.8.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and < 4.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "e03f6ae9a536153f8491e3ef7bb0908f5494756905997611abb204ce23b4d64d"}, "premailex": {:hex, :premailex, "0.3.19", "c26ff9c712c08e574d1792f2cfed638e7c7a5e353b5a4db7a40487c8130fa37c", [:mix], [{:certifi, ">= 0.0.0", [hex: :certifi, repo: "hexpm", optional: true]}, {:floki, "~> 0.19", [hex: :floki, repo: "hexpm", optional: false]}, {:meeseeks, "~> 0.11", [hex: :meeseeks, repo: "hexpm", optional: true]}, {:ssl_verify_fun, ">= 0.0.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: true]}], "hexpm", "18f3772f4b30ffe82f670c2714b2d3221eb9face69e38bb1674b6e570b7c0aff"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, diff --git a/registrations/priv/repo/migrations/20240714173901_create_users.exs b/registrations/priv/repo/migrations/20240714173901_create_users.exs new file mode 100644 index 00000000..867eaa2e --- /dev/null +++ b/registrations/priv/repo/migrations/20240714173901_create_users.exs @@ -0,0 +1,14 @@ +defmodule Registrations.Repo.Migrations.CreateUsers do + use Ecto.Migration + + def change do + create table(:users) do + add :email, :string, null: false + add :password_hash, :string + + timestamps() + end + + create unique_index(:users, [:email]) + end +end From 4c1c813a97b066405f8043608b120db6562081ba Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 12:39:54 -0500 Subject: [PATCH 02/37] Add Pow session to endpoint It failed to insert this automatically, formatting? --- registrations/lib/registrations_web/endpoint.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/registrations/lib/registrations_web/endpoint.ex b/registrations/lib/registrations_web/endpoint.ex index 246314da..a40606ab 100644 --- a/registrations/lib/registrations_web/endpoint.ex +++ b/registrations/lib/registrations_web/endpoint.ex @@ -43,5 +43,7 @@ defmodule RegistrationsWeb.Endpoint do max_age: 60 * 60 * 24 * 365 ) + plug(Pow.Plug.Session, otp_app: :registrations) + plug(RegistrationsWeb.Router) end From dfd435763444276921f70840c567f0eb13d28b48 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 12:47:56 -0500 Subject: [PATCH 03/37] Change migration This adapts the existing table, although do I really need that? Maybe, probably. --- .../migrations/20240714173901_create_users.exs | 14 -------------- .../20240714173901_update_users_for_pow.exs | 11 +++++++++++ 2 files changed, 11 insertions(+), 14 deletions(-) delete mode 100644 registrations/priv/repo/migrations/20240714173901_create_users.exs create mode 100644 registrations/priv/repo/migrations/20240714173901_update_users_for_pow.exs diff --git a/registrations/priv/repo/migrations/20240714173901_create_users.exs b/registrations/priv/repo/migrations/20240714173901_create_users.exs deleted file mode 100644 index 867eaa2e..00000000 --- a/registrations/priv/repo/migrations/20240714173901_create_users.exs +++ /dev/null @@ -1,14 +0,0 @@ -defmodule Registrations.Repo.Migrations.CreateUsers do - use Ecto.Migration - - def change do - create table(:users) do - add :email, :string, null: false - add :password_hash, :string - - timestamps() - end - - create unique_index(:users, [:email]) - end -end diff --git a/registrations/priv/repo/migrations/20240714173901_update_users_for_pow.exs b/registrations/priv/repo/migrations/20240714173901_update_users_for_pow.exs new file mode 100644 index 00000000..9884a92b --- /dev/null +++ b/registrations/priv/repo/migrations/20240714173901_update_users_for_pow.exs @@ -0,0 +1,11 @@ +defmodule Registrations.Repo.Migrations.CreateUsers do + use Ecto.Migration + + def change do + rename table(:users), :crypted_password, to: :password_hash + + alter table(:users) do + modify :email, :string, null: false + end + end +end From 0d0e37352e39d3391228530d2e072b16ab53a2d4 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 13:26:19 -0500 Subject: [PATCH 04/37] Add more blueprint changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I ran the install command in a fresh application to see some of these… sad --- registrations/config/config.exs | 4 ++++ registrations/lib/registrations/users/user.ex | 10 ---------- registrations/lib/registrations_web/models/user.ex | 12 ++++-------- registrations/lib/registrations_web/router.ex | 7 +++++++ 4 files changed, 15 insertions(+), 18 deletions(-) delete mode 100644 registrations/lib/registrations/users/user.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 2038425b..1461586e 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -34,6 +34,10 @@ import_config "#{Mix.env()}.exs" # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason +config :registrations, :pow, + user: Registrations.User, + repo: Registrations.Repo + # Configure phoenix generators config :phoenix, :generators, migration: true, diff --git a/registrations/lib/registrations/users/user.ex b/registrations/lib/registrations/users/user.ex deleted file mode 100644 index 483e483d..00000000 --- a/registrations/lib/registrations/users/user.ex +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Registrations.Users.User do - use Ecto.Schema - use Pow.Ecto.Schema - - schema "users" do - pow_user_fields() - - timestamps() - end -end diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index 2f647d8c..9a974373 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -1,18 +1,14 @@ defmodule RegistrationsWeb.User do + use Ecto.Schema + use Pow.Ecto.Schema + use RegistrationsWeb, :model alias Registrations.Repo @primary_key {:id, :binary_id, autogenerate: true} schema "users" do - field(:email, RegistrationsWeb.DowncasedString) - field(:crypted_password, :string) - field(:password, :string, virtual: true) - field(:recovery_hash, :string) - - field(:new_password, :string, virtual: true) - field(:new_password_confirmation, :string, virtual: true) - field(:current_password, :string, virtual: true) + pow_user_fields() field(:admin, :boolean) diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index f3a6b804..d7297a38 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -1,5 +1,6 @@ defmodule RegistrationsWeb.Router do use RegistrationsWeb, :router + use Pow.Phoenix.Router pipeline :browser do plug(:accepts, ["html"]) @@ -17,6 +18,12 @@ defmodule RegistrationsWeb.Router do plug(RegistrationsWeb.Plugs.CurrentUser) end + scope "/" do + pipe_through :browser + + pow_routes() + end + scope "/", RegistrationsWeb do # Use the default browser stack pipe_through(:browser) From fda8c70d2bbf3385535cf1c95c4dc4c067dc6aea Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 13:32:04 -0500 Subject: [PATCH 05/37] Fix Pow config for module name --- registrations/config/config.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 1461586e..adda72cd 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -35,7 +35,7 @@ import_config "#{Mix.env()}.exs" config :phoenix, :json_library, Jason config :registrations, :pow, - user: Registrations.User, + user: RegistrationsWeb.User, repo: Registrations.Repo # Configure phoenix generators From 19f4112c4fda3439d9d0546fb28df2ddd1bda028 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 13:46:31 -0500 Subject: [PATCH 06/37] Rename existing User changeset function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was clashing and the hash wasn’t saving. --- .../registrations_web/controllers/registration_controller.ex | 4 ++-- registrations/lib/registrations_web/models/user.ex | 2 +- registrations/test/models/user_test.exs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/registrations/lib/registrations_web/controllers/registration_controller.ex b/registrations/lib/registrations_web/controllers/registration_controller.ex index 1f857526..bcdc6a62 100644 --- a/registrations/lib/registrations_web/controllers/registration_controller.ex +++ b/registrations/lib/registrations_web/controllers/registration_controller.ex @@ -3,7 +3,7 @@ defmodule RegistrationsWeb.RegistrationController do alias RegistrationsWeb.User def new(conn, _params) do - changeset = User.changeset(%User{}) + changeset = User.old_changeset(%User{}) conn = case Application.get_env(:registrations, :registration_closed) do @@ -22,7 +22,7 @@ defmodule RegistrationsWeb.RegistrationController do end def create(conn, %{"user" => user_params}) do - changeset = User.changeset(%User{}, user_params) + changeset = User.old_changeset(%User{}, user_params) case RegistrationsWeb.Registration.create(changeset, Registrations.Repo) do {:ok, user} -> diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index 9a974373..1e5b8dd0 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -40,7 +40,7 @@ defmodule RegistrationsWeb.User do If no params are provided, an invalid changeset is returned with no validation performed. """ - def changeset(model, params \\ %{}) do + def old_changeset(model, params \\ %{}) do model |> cast(params, @required_fields ++ @optional_fields) |> validate_required(@required_fields) diff --git a/registrations/test/models/user_test.exs b/registrations/test/models/user_test.exs index aba67b87..fad08fdd 100644 --- a/registrations/test/models/user_test.exs +++ b/registrations/test/models/user_test.exs @@ -8,12 +8,12 @@ defmodule RegistrationsWeb.UserTest do @invalid_attrs %{} test "changeset with valid attributes" do - changeset = User.changeset(%User{}, @valid_attrs) + changeset = User.old_changeset(%User{}, @valid_attrs) assert changeset.valid? end test "changeset with invalid attributes" do - changeset = User.changeset(%User{}, @invalid_attrs) + changeset = User.old_changeset(%User{}, @invalid_attrs) refute changeset.valid? end From 8d2c2e7468de312e60e729855be7deb77c100b3e Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 14:51:19 -0500 Subject: [PATCH 07/37] Add overridden templates --- registrations/config/config.exs | 1 + .../templates/pow/registration/edit.html.eex | 37 +++++++++++++++++++ .../templates/pow/registration/new.html.eex | 32 ++++++++++++++++ .../templates/pow/session/new.html.eex | 28 ++++++++++++++ .../views/pow/registration_view.ex | 3 ++ .../views/pow/session_view.ex | 3 ++ 6 files changed, 104 insertions(+) create mode 100644 registrations/lib/registrations_web/templates/pow/registration/edit.html.eex create mode 100644 registrations/lib/registrations_web/templates/pow/registration/new.html.eex create mode 100644 registrations/lib/registrations_web/templates/pow/session/new.html.eex create mode 100644 registrations/lib/registrations_web/views/pow/registration_view.ex create mode 100644 registrations/lib/registrations_web/views/pow/session_view.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index adda72cd..22bd0c2f 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -35,6 +35,7 @@ import_config "#{Mix.env()}.exs" config :phoenix, :json_library, Jason config :registrations, :pow, + web_module: RegistrationsWeb, user: RegistrationsWeb.User, repo: Registrations.Repo diff --git a/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex new file mode 100644 index 00000000..f6fc6a56 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex @@ -0,0 +1,37 @@ +
+
+

Change Password

+ + <%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> + +
+
+
+ <%= label f, :current_password %> + <%= password_input f, :current_password %> + <%= error_tag f, :current_password %> +
+
+ <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %> +
+
+ <%= label f, :password %> + <%= password_input f, :password %> + <%= error_tag f, :password %> +
+
+ <%= label f, :password_confirmation %> + <%= password_input f, :password_confirmation %> + <%= error_tag f, :password_confirmation %> +
+
+ <%= submit "Change password", class: "button primary" %> +
+
+
+ <% end %> +
+
diff --git a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex new file mode 100644 index 00000000..9d96fe34 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex @@ -0,0 +1,32 @@ +
+
+

Register

+ + <%= form_for @changeset, Routes.registration_path(@conn, :create), fn f -> %> + <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> + +
+
+
+ <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %> +
+
+ <%= label f, :password %> + <%= password_input f, :password %> + <%= error_tag f, :password %> +
+
+ <%= label f, :password_confirmation %> + <%= password_input f, :password_confirmation %> + <%= error_tag f, :password_confirmation %> +
+
+ <%= submit "Register", class: "button primary" %> +
+
+
+ <% end %> +
+
diff --git a/registrations/lib/registrations_web/templates/pow/session/new.html.eex b/registrations/lib/registrations_web/templates/pow/session/new.html.eex new file mode 100644 index 00000000..242446f7 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow/session/new.html.eex @@ -0,0 +1,28 @@ +
+
+

Sign in

+ + + <%= link "Forgot your password?", to: Routes.reset_path(@conn, :new), class: "forgot" %> + <%= link "Register", to: Routes.pow_registration_path(@conn, :new) %> + <%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> + +
+ <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %> +
+ +
+ <%= label f, :password %> + <%= password_input f, :password %> + <%= error_tag f, :password %> +
+ +
+ <%= submit "Sign in", class: "button primary" %> +
+ <% end %> +
+
diff --git a/registrations/lib/registrations_web/views/pow/registration_view.ex b/registrations/lib/registrations_web/views/pow/registration_view.ex new file mode 100644 index 00000000..99b86095 --- /dev/null +++ b/registrations/lib/registrations_web/views/pow/registration_view.ex @@ -0,0 +1,3 @@ +defmodule RegistrationsWeb.Pow.RegistrationView do + use RegistrationsWeb, :view +end diff --git a/registrations/lib/registrations_web/views/pow/session_view.ex b/registrations/lib/registrations_web/views/pow/session_view.ex new file mode 100644 index 00000000..1736c679 --- /dev/null +++ b/registrations/lib/registrations_web/views/pow/session_view.ex @@ -0,0 +1,3 @@ +defmodule RegistrationsWeb.Pow.SessionView do + use RegistrationsWeb, :view +end From 98c9a3b267bed6d91f826aed0f5f5f5907878d76 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 15:33:32 -0500 Subject: [PATCH 08/37] =?UTF-8?q?Change=20to=20using=20Pow=E2=80=99s=20ses?= =?UTF-8?q?sion=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mostly? --- .../controllers/message_controller.ex | 2 +- .../controllers/registration_controller.ex | 8 +++---- .../controllers/user_controller.ex | 6 ++--- .../lib/registrations_web/models/session.ex | 2 +- .../lib/registrations_web/plugs/admin.ex | 2 +- .../registrations_web/plugs/current_user.ex | 23 ------------------- .../registrations_web/plugs/login_required.ex | 2 +- registrations/lib/registrations_web/router.ex | 2 -- 8 files changed, 11 insertions(+), 36 deletions(-) delete mode 100644 registrations/lib/registrations_web/plugs/current_user.ex diff --git a/registrations/lib/registrations_web/controllers/message_controller.ex b/registrations/lib/registrations_web/controllers/message_controller.ex index 59d19122..65980e4d 100644 --- a/registrations/lib/registrations_web/controllers/message_controller.ex +++ b/registrations/lib/registrations_web/controllers/message_controller.ex @@ -68,7 +68,7 @@ defmodule RegistrationsWeb.MessageController do users = if(me == "true", - do: [conn.assigns[:current_user_object]], + do: [conn.assigns[:current_user]], else: Repo.all(RegistrationsWeb.User) ) |> Repo.preload(team: [:users]) diff --git a/registrations/lib/registrations_web/controllers/registration_controller.ex b/registrations/lib/registrations_web/controllers/registration_controller.ex index bcdc6a62..cd562b33 100644 --- a/registrations/lib/registrations_web/controllers/registration_controller.ex +++ b/registrations/lib/registrations_web/controllers/registration_controller.ex @@ -55,14 +55,14 @@ defmodule RegistrationsWeb.RegistrationController do end def edit(conn, _) do - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] changeset = User.account_changeset(current_user) render(conn, "edit.html", user: current_user, changeset: changeset) end def update(conn, %{"user" => user_params}) do - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] changeset = User.account_changeset(current_user, user_params) session_params = %{ @@ -93,14 +93,14 @@ defmodule RegistrationsWeb.RegistrationController do # FIXME 😳 def maybe_delete(conn, _) do - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] changeset = User.deletion_changeset(current_user) render(conn, "maybe_delete.html", user: current_user, changeset: changeset) end def delete(conn, %{"user" => user_params}) do - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] changeset = User.deletion_changeset(current_user, user_params) session_params = %{ diff --git a/registrations/lib/registrations_web/controllers/user_controller.ex b/registrations/lib/registrations_web/controllers/user_controller.ex index dc4aba67..7663ca50 100644 --- a/registrations/lib/registrations_web/controllers/user_controller.ex +++ b/registrations/lib/registrations_web/controllers/user_controller.ex @@ -27,7 +27,7 @@ defmodule RegistrationsWeb.UserController do def edit(conn, _) do users = Repo.all(User) - current_user_only = conn.assigns[:current_user_object] + current_user_only = conn.assigns[:current_user] changeset = User.details_changeset(current_user_only) current_user = Repo.preload(current_user_only, team: [:users]) @@ -55,7 +55,7 @@ defmodule RegistrationsWeb.UserController do def update(conn, %{"user" => user_params}) do users = Repo.all(User) - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] changeset = User.details_changeset(current_user, user_params) current_user = Repo.preload(current_user, team: [:users]) @@ -79,7 +79,7 @@ defmodule RegistrationsWeb.UserController do end def voicepass(conn, _params) do - current_user = conn.assigns[:current_user_object] + current_user = conn.assigns[:current_user] lines = User.voicepass_candidates() random_index = :rand.uniform(length(lines)) diff --git a/registrations/lib/registrations_web/models/session.ex b/registrations/lib/registrations_web/models/session.ex index 62ecf27d..c8363ea5 100644 --- a/registrations/lib/registrations_web/models/session.ex +++ b/registrations/lib/registrations_web/models/session.ex @@ -18,7 +18,7 @@ defmodule RegistrationsWeb.Session do end def current_user(conn) do - conn.assigns[:current_user_object] + conn.assigns[:current_user] end def logged_in?(conn), do: !!current_user(conn) diff --git a/registrations/lib/registrations_web/plugs/admin.ex b/registrations/lib/registrations_web/plugs/admin.ex index bac3a1af..eb9ea220 100644 --- a/registrations/lib/registrations_web/plugs/admin.ex +++ b/registrations/lib/registrations_web/plugs/admin.ex @@ -7,7 +7,7 @@ defmodule RegistrationsWeb.Plugs.Admin do def call(conn, _) do conn = fetch_session(conn) - user = conn.assigns[:current_user_object] + user = conn.assigns[:current_user] if user && user.admin do conn diff --git a/registrations/lib/registrations_web/plugs/current_user.ex b/registrations/lib/registrations_web/plugs/current_user.ex deleted file mode 100644 index 9fcb4187..00000000 --- a/registrations/lib/registrations_web/plugs/current_user.ex +++ /dev/null @@ -1,23 +0,0 @@ -# Adapted from Addict -# https://github.com/trenpixster/addict/blob/master/lib/addict/plugs/authenticated.ex - -defmodule RegistrationsWeb.Plugs.CurrentUser do - import Plug.Conn - - def init(options) do - options - end - - def call(conn, _) do - conn = fetch_session(conn) - - id = get_session(conn, :current_user) - - if id do - user = Registrations.Repo.get(RegistrationsWeb.User, id) - assign(conn, :current_user_object, user) - else - conn - end - end -end diff --git a/registrations/lib/registrations_web/plugs/login_required.ex b/registrations/lib/registrations_web/plugs/login_required.ex index 26a63b55..8c6412d0 100644 --- a/registrations/lib/registrations_web/plugs/login_required.ex +++ b/registrations/lib/registrations_web/plugs/login_required.ex @@ -7,7 +7,7 @@ defmodule RegistrationsWeb.Plugs.LoginRequired do def call(conn, _) do conn = fetch_session(conn) - user = conn.assigns[:current_user_object] + user = conn.assigns[:current_user] if user do conn diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index d7297a38..68ac526a 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -8,14 +8,12 @@ defmodule RegistrationsWeb.Router do plug(:fetch_flash) plug(:protect_from_forgery) plug(:put_secure_browser_headers) - plug(RegistrationsWeb.Plugs.CurrentUser) plug(RegistrationsWeb.Plugs.Settings) end pipeline :api do plug(:accepts, ["json"]) plug(:fetch_session) - plug(RegistrationsWeb.Plugs.CurrentUser) end scope "/" do From 8230b21762f0e4fcdd5efccac0d1f951f96d7523 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 15:41:51 -0500 Subject: [PATCH 09/37] Change some links --- .../lib/registrations_web/templates/layout/app.html.heex | 6 +++--- .../templates/page/clandestine-rendezvous.html.heex | 6 +++--- .../templates/page/unmnemonic-devices.html.heex | 6 +++--- .../templates/pow/registration/new.html.eex | 2 +- .../registrations_web/templates/registration/edit.html.heex | 2 +- .../templates/registration/maybe_delete.html.heex | 2 +- .../registrations_web/templates/registration/new.html.heex | 2 +- .../registrations_web/templates/shared/question.html.heex | 2 +- .../lib/registrations_web/templates/user/edit.html.heex | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/registrations/lib/registrations_web/templates/layout/app.html.heex b/registrations/lib/registrations_web/templates/layout/app.html.heex index f31b6f28..ad30cb4c 100644 --- a/registrations/lib/registrations_web/templates/layout/app.html.heex +++ b/registrations/lib/registrations_web/templates/layout/app.html.heex @@ -31,7 +31,7 @@
  • <%= active_link "Home", @conn, Routes.page_path(@conn, :index), class: "home" %>
  • <%= if logged_in?(@conn) do %>
  • - <%= link to: Routes.session_path(@conn, :delete), method: :delete, class: "logout" do %> + <%= link to: Routes.pow_session_path(@conn, :delete), method: :delete, class: "logout" do %> Log out <%= current_user(@conn).email %> <% end %>
  • @@ -45,8 +45,8 @@ <% end %> <% end %> <% else %> -
  • <%= active_link "Register", @conn, Routes.registration_path(@conn, :new), class: "register" %>
  • -
  • <%= active_link "Log in", @conn, Routes.session_path(@conn, :new), class: "login" %>
  • +
  • <%= active_link "Register", @conn, Routes.pow_registration_path(@conn, :new), class: "register" %>
  • +
  • <%= active_link "Log in", @conn, Routes.pow_session_path(@conn, :new), class: "login" %>
  • <% end %> diff --git a/registrations/lib/registrations_web/templates/page/clandestine-rendezvous.html.heex b/registrations/lib/registrations_web/templates/page/clandestine-rendezvous.html.heex index 34ac9dbd..428e3a9d 100644 --- a/registrations/lib/registrations_web/templates/page/clandestine-rendezvous.html.heex +++ b/registrations/lib/registrations_web/templates/page/clandestine-rendezvous.html.heex @@ -29,7 +29,7 @@

    When and where?

    -

    The secret society is so secret that the venue for initiation cannot be shared publicly. Once it is decided, it will be shared with those who <%= link "register", to: Routes.registration_path(@conn, :new) %>. If you have questions about travel, please write to us after registering.

    +

    The secret society is so secret that the venue for initiation cannot be shared publicly. Once it is decided, it will be shared with those who <%= link "register", to: Routes.pow_registration_path(@conn, :new) %>. If you have questions about travel, please write to us after registering.

    The adventure will begin at <%= start_time() %>. If you have obligations that prevent you from being present at the beginning let us know and we can discuss how to accommodate you.

    @@ -51,13 +51,13 @@

    Accessibility

    -

    Do you have accessibility needs we should know about? Please tell us <%= link "when you register", to: Routes.registration_path(@conn, :new) %> and we will work with you to ensure you can participate.

    +

    Do you have accessibility needs we should know about? Please tell us <%= link "when you register", to: Routes.pow_registration_path(@conn, :new) %> and we will work with you to ensure you can participate.

    Registration

    -

    You must <%= link "register", to: Routes.registration_path(@conn, :new) %> to attend the event, as scheduling each rendezvous depends on knowing who will attend. If you’re uncertain whether you can make it, register anyway; you can cancel up until the day before.

    +

    You must <%= link "register", to: Routes.pow_registration_path(@conn, :new) %> to attend the event, as scheduling each rendezvous depends on knowing who will attend. If you’re uncertain whether you can make it, register anyway; you can cancel up until the day before.

    diff --git a/registrations/lib/registrations_web/templates/page/unmnemonic-devices.html.heex b/registrations/lib/registrations_web/templates/page/unmnemonic-devices.html.heex index 33366bfd..192d51fa 100644 --- a/registrations/lib/registrations_web/templates/page/unmnemonic-devices.html.heex +++ b/registrations/lib/registrations_web/templates/page/unmnemonic-devices.html.heex @@ -57,7 +57,7 @@

    Tell me more!

    To learn more about our technique, please - <%= link "register", to: Routes.registration_path(@conn, :new) %> + <%= link "register", to: Routes.pow_registration_path(@conn, :new) %> to attend the participatory demonstration evening on FIXME, location to be announced.

    @@ -96,7 +96,7 @@

    Accessibility

    Do you have accessibility needs we should know about? Please tell us - <%= link "when you register", to: Routes.registration_path(@conn, :new) %> + <%= link "when you register", to: Routes.pow_registration_path(@conn, :new) %> and we will work with you to ensure you can participate.

    @@ -107,7 +107,7 @@

    Registration

    You must - <%= link "register", to: Routes.registration_path(@conn, :new) %> + <%= link "register", to: Routes.pow_registration_path(@conn, :new) %> to attend the event, as each therapeutic journey is hand-tuned. If you’re uncertain whether you can make it, register anyway; you can cancel up until the day before. diff --git a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex index 9d96fe34..0d8db6ab 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex @@ -2,7 +2,7 @@

    Register

    - <%= form_for @changeset, Routes.registration_path(@conn, :create), fn f -> %> + <%= form_for @changeset, Routes.pow_registration_path(@conn, :create), fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %>
    diff --git a/registrations/lib/registrations_web/templates/registration/edit.html.heex b/registrations/lib/registrations_web/templates/registration/edit.html.heex index dab77697..5a7b5463 100644 --- a/registrations/lib/registrations_web/templates/registration/edit.html.heex +++ b/registrations/lib/registrations_web/templates/registration/edit.html.heex @@ -2,7 +2,7 @@

    Change Password

    - <%= form_for @changeset, Routes.registration_path(@conn, :update), fn f -> %> + <%= form_for @changeset, Routes.pow_registration_path(@conn, :update), fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %>
    diff --git a/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex b/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex index d2fdc1bf..806ebb8a 100644 --- a/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex +++ b/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex @@ -2,7 +2,7 @@

    Delete account

    - <%= form_for @changeset, Routes.registration_path(@conn, :delete), fn f -> %> + <%= form_for @changeset, Routes.pow_registration_path(@conn, :delete), fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %>
    diff --git a/registrations/lib/registrations_web/templates/registration/new.html.heex b/registrations/lib/registrations_web/templates/registration/new.html.heex index 3027f713..385a51df 100644 --- a/registrations/lib/registrations_web/templates/registration/new.html.heex +++ b/registrations/lib/registrations_web/templates/registration/new.html.heex @@ -2,7 +2,7 @@

    Register

    - <%= form_for @changeset, Routes.registration_path(@conn, :create), fn f -> %> + <%= form_for @changeset, Routes.pow_registration_path(@conn, :create), fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %>
    diff --git a/registrations/lib/registrations_web/templates/shared/question.html.heex b/registrations/lib/registrations_web/templates/shared/question.html.heex index 98abc1fb..912def72 100644 --- a/registrations/lib/registrations_web/templates/shared/question.html.heex +++ b/registrations/lib/registrations_web/templates/shared/question.html.heex @@ -1,4 +1,4 @@ -

    This is not the registration form, that’s <%= link "here", to: Routes.registration_path(@conn, :new) %>.

    +

    This is not the registration form, that’s <%= link "here", to: Routes.pow_registration_path(@conn, :new) %>.

    <%= form_for @conn, Routes.page_path(@conn, :questions), [as: :question], fn f -> %>
    diff --git a/registrations/lib/registrations_web/templates/user/edit.html.heex b/registrations/lib/registrations_web/templates/user/edit.html.heex index 62a7f71e..94244e54 100644 --- a/registrations/lib/registrations_web/templates/user/edit.html.heex +++ b/registrations/lib/registrations_web/templates/user/edit.html.heex @@ -96,7 +96,7 @@ <%= render "relationships.html", user: @user, relationships: @relationships %>
    - <%= link "Change your password", to: Routes.registration_path(@conn, :edit), class: "account" %> + <%= link "Change your password", to: Routes.pow_registration_path(@conn, :edit), class: "account" %>
    From d692755cfd38542eda6725257b44a214e5b78c1f Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:16:37 -0500 Subject: [PATCH 10/37] Remove unused files --- .../controllers/registration_controller.ex | 133 ------------------ .../registrations_web/models/registration.ex | 25 ---- .../lib/registrations_web/models/reset.ex | 24 ---- .../views/registration_view.ex | 3 - .../lib/registrations_web/views/reset_view.ex | 3 - .../registrations_web/views/session_view.ex | 3 - 6 files changed, 191 deletions(-) delete mode 100644 registrations/lib/registrations_web/controllers/registration_controller.ex delete mode 100644 registrations/lib/registrations_web/models/registration.ex delete mode 100644 registrations/lib/registrations_web/models/reset.ex delete mode 100644 registrations/lib/registrations_web/views/registration_view.ex delete mode 100644 registrations/lib/registrations_web/views/reset_view.ex delete mode 100644 registrations/lib/registrations_web/views/session_view.ex diff --git a/registrations/lib/registrations_web/controllers/registration_controller.ex b/registrations/lib/registrations_web/controllers/registration_controller.ex deleted file mode 100644 index cd562b33..00000000 --- a/registrations/lib/registrations_web/controllers/registration_controller.ex +++ /dev/null @@ -1,133 +0,0 @@ -defmodule RegistrationsWeb.RegistrationController do - use RegistrationsWeb, :controller - alias RegistrationsWeb.User - - def new(conn, _params) do - changeset = User.old_changeset(%User{}) - - conn = - case Application.get_env(:registrations, :registration_closed) do - true -> - conn - |> put_flash( - :error, - "Registration is closed; however, you may continue and we will email you" - ) - - _ -> - conn - end - - render(conn, changeset: changeset) - end - - def create(conn, %{"user" => user_params}) do - changeset = User.old_changeset(%User{}, user_params) - - case RegistrationsWeb.Registration.create(changeset, Registrations.Repo) do - {:ok, user} -> - messages = - Registrations.Repo.all( - from(m in RegistrationsWeb.Message, - where: m.ready == true, - select: m, - order_by: :postmarked_at - ) - ) - - Registrations.Mailer.send_registration(user) - Registrations.Mailer.send_welcome_email(user.email) - - unless Enum.empty?(messages) do - Registrations.Mailer.send_backlog(messages, user) - end - - conn - |> put_session(:current_user, user.id) - |> put_flash(:info, "Your account was created") - |> redirect(to: Routes.user_path(conn, :edit)) - - {:error, changeset} -> - conn - |> put_flash(:error, "Unable to create account") - |> render("new.html", changeset: changeset) - end - end - - def edit(conn, _) do - current_user = conn.assigns[:current_user] - changeset = User.account_changeset(current_user) - - render(conn, "edit.html", user: current_user, changeset: changeset) - end - - def update(conn, %{"user" => user_params}) do - current_user = conn.assigns[:current_user] - changeset = User.account_changeset(current_user, user_params) - - session_params = %{ - "email" => current_user.email, - "password" => user_params["current_password"] - } - - case RegistrationsWeb.Session.login(session_params, Registrations.Repo) do - {:ok, _} -> - case RegistrationsWeb.Registration.update(changeset, Registrations.Repo) do - {:ok, _} -> - conn - |> put_flash(:info, "Your password has been changed") - |> redirect(to: Routes.user_path(conn, :edit)) - - {:error, changeset} -> - conn - |> put_flash(:error, "New passwords must match") - |> render("edit.html", changeset: changeset) - end - - :error -> - conn - |> put_flash(:error, "Please enter your current password") - |> render("edit.html", changeset: changeset) - end - end - - # FIXME 😳 - def maybe_delete(conn, _) do - current_user = conn.assigns[:current_user] - changeset = User.deletion_changeset(current_user) - - render(conn, "maybe_delete.html", user: current_user, changeset: changeset) - end - - def delete(conn, %{"user" => user_params}) do - current_user = conn.assigns[:current_user] - changeset = User.deletion_changeset(current_user, user_params) - - session_params = %{ - "email" => current_user.email, - "password" => user_params["current_password"] - } - - case RegistrationsWeb.Session.login(session_params, Registrations.Repo) do - {:ok, _} -> - case RegistrationsWeb.Registration.delete(changeset, Registrations.Repo) do - {:ok, _} -> - Registrations.Mailer.send_user_deletion(current_user) - - conn - |> put_flash(:info, "Your account has been deleted 😧") - |> redirect(to: Routes.page_path(conn, :index)) - - {:error, changeset} -> - conn - |> put_flash(:error, "Something went wrong!") - |> render("maybe_delete.html", changeset: changeset) - end - - :error -> - conn - |> put_flash(:error, "Your password did not match") - |> render("maybe_delete.html", changeset: changeset) - end - end -end diff --git a/registrations/lib/registrations_web/models/registration.ex b/registrations/lib/registrations_web/models/registration.ex deleted file mode 100644 index 43ab9fa3..00000000 --- a/registrations/lib/registrations_web/models/registration.ex +++ /dev/null @@ -1,25 +0,0 @@ -defmodule RegistrationsWeb.Registration do - import Ecto.Changeset, only: [put_change: 3] - - def create(changeset, repo) do - changeset - |> put_change(:crypted_password, hashed_password(changeset.params["password"])) - |> repo.insert() - end - - def update(changeset, repo) do - changeset - |> put_change(:crypted_password, hashed_password(changeset.params["new_password"])) - |> repo.update() - end - - def delete(changeset, repo) do - changeset - |> repo.delete - end - - # FIXME this was private but now shared with Reset! - def hashed_password(password) do - Bcrypt.hash_pwd_salt(password) - end -end diff --git a/registrations/lib/registrations_web/models/reset.ex b/registrations/lib/registrations_web/models/reset.ex deleted file mode 100644 index e761d2ba..00000000 --- a/registrations/lib/registrations_web/models/reset.ex +++ /dev/null @@ -1,24 +0,0 @@ -defmodule RegistrationsWeb.Reset do - import Ecto.Changeset, only: [put_change: 3] - alias RegistrationsWeb.User - - def create(user, repo) do - if user do - User.reset_changeset(user) - |> put_change(:recovery_hash, Bcrypt.hash_pwd_salt("1")) - |> repo.update() - else - {:error, :user_not_found} - end - end - - def update(changeset, repo) do - changeset - |> put_change( - :crypted_password, - RegistrationsWeb.Registration.hashed_password(changeset.params["new_password"]) - ) - |> put_change(:recovery_hash, nil) - |> repo.update() - end -end diff --git a/registrations/lib/registrations_web/views/registration_view.ex b/registrations/lib/registrations_web/views/registration_view.ex deleted file mode 100644 index 89dab643..00000000 --- a/registrations/lib/registrations_web/views/registration_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule RegistrationsWeb.RegistrationView do - use RegistrationsWeb, :view -end diff --git a/registrations/lib/registrations_web/views/reset_view.ex b/registrations/lib/registrations_web/views/reset_view.ex deleted file mode 100644 index 2e048add..00000000 --- a/registrations/lib/registrations_web/views/reset_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule RegistrationsWeb.ResetView do - use RegistrationsWeb, :view -end diff --git a/registrations/lib/registrations_web/views/session_view.ex b/registrations/lib/registrations_web/views/session_view.ex deleted file mode 100644 index 6b1d955e..00000000 --- a/registrations/lib/registrations_web/views/session_view.ex +++ /dev/null @@ -1,3 +0,0 @@ -defmodule RegistrationsWeb.SessionView do - use RegistrationsWeb, :view -end From 87ebe688945434213bc2d7e495742f3ec8a80893 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:16:50 -0500 Subject: [PATCH 11/37] Change crypted_password references --- registrations/test/integration/messages_test.exs | 8 ++++---- registrations/test/integration/teams_test.exs | 14 +++++++------- registrations/test/support/factory.ex | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/registrations/test/integration/messages_test.exs b/registrations/test/integration/messages_test.exs index 0a72152c..c001f0fa 100644 --- a/registrations/test/integration/messages_test.exs +++ b/registrations/test/integration/messages_test.exs @@ -14,7 +14,7 @@ defmodule Registrations.Integration.Messages do test "a message is sent to all registrants with their team information summarised" do insert(:admin, email: "admin@example.com", - crypted_password: Bcrypt.hash_pwd_salt("admin") + password_hash: Bcrypt.hash_pwd_salt("admin") ) insert(:user, @@ -64,7 +64,7 @@ defmodule Registrations.Integration.Messages do test "a message can be sent to just the logged-in user" do insert(:admin, email: "admin@example.com", - crypted_password: Bcrypt.hash_pwd_salt("admin") + password_hash: Bcrypt.hash_pwd_salt("admin") ) insert(:user, @@ -107,7 +107,7 @@ defmodule Registrations.Integration.Messages do test "message sender name/address can be overridden" do insert(:admin, email: "admin@example.com", - crypted_password: Bcrypt.hash_pwd_salt("admin") + password_hash: Bcrypt.hash_pwd_salt("admin") ) navigate_to("/") @@ -139,7 +139,7 @@ defmodule Registrations.Integration.Messages do test "a message with show team enabled shows the actual team information instead of their details" do insert(:admin, email: "admin@example.com", - crypted_password: Bcrypt.hash_pwd_salt("admin") + password_hash: Bcrypt.hash_pwd_salt("admin") ) team = diff --git a/registrations/test/integration/teams_test.exs b/registrations/test/integration/teams_test.exs index 11c42eb8..d5a02c34 100644 --- a/registrations/test/integration/teams_test.exs +++ b/registrations/test/integration/teams_test.exs @@ -38,7 +38,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) navigate_to("/") @@ -151,7 +151,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) navigate_to("/") @@ -168,7 +168,7 @@ defmodule Registrations.Integration.Teams do test "the table is hidden when empty" do insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) navigate_to("/") @@ -183,7 +183,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) navigate_to("/") @@ -221,11 +221,11 @@ defmodule Registrations.Integration.Teams do takver = insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) bedap = - insert(:user, email: "bedap@example.com", crypted_password: Bcrypt.hash_pwd_salt("Anarres")) + insert(:user, email: "bedap@example.com", password_hash: Bcrypt.hash_pwd_salt("Anarres")) insert(:team, name: "A team", @@ -270,7 +270,7 @@ defmodule Registrations.Integration.UnmnemonicDevices.Teams do insert(:user, email: "takver@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Bcrypt.hash_pwd_salt("Anarres") ) navigate_to("/") diff --git a/registrations/test/support/factory.ex b/registrations/test/support/factory.ex index 7e69fba6..7b1ff6f9 100644 --- a/registrations/test/support/factory.ex +++ b/registrations/test/support/factory.ex @@ -21,7 +21,7 @@ defmodule Registrations.Factory do user_factory(), %{ email: "octavia.butler@example.com", - crypted_password: Bcrypt.hash_pwd_salt("Xenogenesis") + password_hash: Bcrypt.hash_pwd_salt("Xenogenesis") } ) end From 3d0758022cbd0d3994f9a6ba82e3c93fb840373d Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:30:53 -0500 Subject: [PATCH 12/37] Add some form ids MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test selectors would be nice… but meh --- .../templates/pow/registration/edit.html.eex | 8 ++++---- .../templates/pow/registration/new.html.eex | 4 ++-- .../registrations_web/templates/pow/session/new.html.eex | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex index f6fc6a56..12295e2c 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex @@ -9,22 +9,22 @@
    <%= label f, :current_password %> - <%= password_input f, :current_password %> + <%= password_input f, :current_password, id: 'current-password' %> <%= error_tag f, :current_password %>
    <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> - <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset), id: 'email' %> <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
    <%= label f, :password %> - <%= password_input f, :password %> + <%= password_input f, :password, id: 'password' %> <%= error_tag f, :password %>
    <%= label f, :password_confirmation %> - <%= password_input f, :password_confirmation %> + <%= password_input f, :password_confirmation, id: 'password-confirmation' %> <%= error_tag f, :password_confirmation %>
    diff --git a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex index 0d8db6ab..99039bef 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex @@ -14,12 +14,12 @@
    <%= label f, :password %> - <%= password_input f, :password %> + <%= password_input f, :password, id: 'password' %> <%= error_tag f, :password %>
    <%= label f, :password_confirmation %> - <%= password_input f, :password_confirmation %> + <%= password_input f, :password_confirmation, id: 'password-confirmation' %> <%= error_tag f, :password_confirmation %>
    diff --git a/registrations/lib/registrations_web/templates/pow/session/new.html.eex b/registrations/lib/registrations_web/templates/pow/session/new.html.eex index 242446f7..62a37e15 100644 --- a/registrations/lib/registrations_web/templates/pow/session/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/session/new.html.eex @@ -10,13 +10,13 @@
    <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> - <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset), id: 'email' %> <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
    <%= label f, :password %> - <%= password_input f, :password %> + <%= password_input f, :password, id: 'password' %> <%= error_tag f, :password %>
    From 5f649f5e3378bad3865ee5214a1158141d5220e4 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:34:14 -0500 Subject: [PATCH 13/37] Remove unused routes --- registrations/lib/registrations_web/router.ex | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index 68ac526a..c4b85120 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -26,13 +26,6 @@ defmodule RegistrationsWeb.Router do # Use the default browser stack pipe_through(:browser) - get("/register", RegistrationController, :new) - post("/register", RegistrationController, :create) - - get("/login", SessionController, :new) - post("/login", SessionController, :create) - delete("/logout", SessionController, :delete) - post("/teams/build", TeamController, :build) resources("/teams", TeamController) @@ -40,17 +33,6 @@ defmodule RegistrationsWeb.Router do get("/details", UserController, :edit) put("/details", UserController, :update) - get("/account", RegistrationController, :edit) - put("/account", RegistrationController, :update) - - get("/delete", RegistrationController, :maybe_delete) - put("/delete", RegistrationController, :delete) - - get("/reset", ResetController, :new) - post("/reset", ResetController, :create) - get("/reset/:token", ResetController, :edit) - put("/reset/:token", ResetController, :update) - resources("/messages", MessageController) post("/messages/send-backlog", MessageController, :deliver_backlog) post("/messages/:id/send", MessageController, :deliver) From a73ebbbc5b18a31b51ae24493a7784b1d6a28156 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:59:04 -0500 Subject: [PATCH 14/37] Fix post-login routing --- registrations/config/config.exs | 3 ++- registrations/lib/registrations_web/routes.ex | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 registrations/lib/registrations_web/routes.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 22bd0c2f..6e3f1ee4 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -37,7 +37,8 @@ config :phoenix, :json_library, Jason config :registrations, :pow, web_module: RegistrationsWeb, user: RegistrationsWeb.User, - repo: Registrations.Repo + repo: Registrations.Repo, + routes_backend: RegistrationsWeb.Pow.Routes # Configure phoenix generators config :phoenix, :generators, diff --git a/registrations/lib/registrations_web/routes.ex b/registrations/lib/registrations_web/routes.ex new file mode 100644 index 00000000..a97a4e2b --- /dev/null +++ b/registrations/lib/registrations_web/routes.ex @@ -0,0 +1,6 @@ +defmodule RegistrationsWeb.Pow.Routes do + use Pow.Phoenix.Routes + + @impl true + def after_sign_in_path(conn), do: RegistrationsWeb.Router.Helpers.user_path(conn, :edit) +end From 476bf8423f7dfcefce90385e344b40a04a3ce14e Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 17:59:14 -0500 Subject: [PATCH 15/37] Remove currently-broken links --- .../lib/registrations_web/templates/pow/session/new.html.eex | 2 +- .../lib/registrations_web/templates/user/edit.html.heex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/registrations/lib/registrations_web/templates/pow/session/new.html.eex b/registrations/lib/registrations_web/templates/pow/session/new.html.eex index 62a37e15..c3508ed0 100644 --- a/registrations/lib/registrations_web/templates/pow/session/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/session/new.html.eex @@ -3,7 +3,7 @@

    Sign in

    - <%= link "Forgot your password?", to: Routes.reset_path(@conn, :new), class: "forgot" %> + <%= link "Register", to: Routes.pow_registration_path(@conn, :new) %> <%= form_for @changeset, @action, [as: :user], fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> diff --git a/registrations/lib/registrations_web/templates/user/edit.html.heex b/registrations/lib/registrations_web/templates/user/edit.html.heex index 94244e54..147a89ee 100644 --- a/registrations/lib/registrations_web/templates/user/edit.html.heex +++ b/registrations/lib/registrations_web/templates/user/edit.html.heex @@ -101,6 +101,6 @@
    - <%= link "Delete your account", to: Routes.registration_path(@conn, :maybe_delete), class: "delete" %> +
    From 928381b7682f6151d9f7c98e79b873b1ab30826c Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 18:40:59 -0500 Subject: [PATCH 16/37] Fix setup of accounts in tests --- .../test/integration/messages_test.exs | 8 ++++---- registrations/test/integration/teams_test.exs | 17 ++++++++++------- registrations/test/support/factory.ex | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/registrations/test/integration/messages_test.exs b/registrations/test/integration/messages_test.exs index c001f0fa..6f184b49 100644 --- a/registrations/test/integration/messages_test.exs +++ b/registrations/test/integration/messages_test.exs @@ -14,7 +14,7 @@ defmodule Registrations.Integration.Messages do test "a message is sent to all registrants with their team information summarised" do insert(:admin, email: "admin@example.com", - password_hash: Bcrypt.hash_pwd_salt("admin") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("admin") ) insert(:user, @@ -64,7 +64,7 @@ defmodule Registrations.Integration.Messages do test "a message can be sent to just the logged-in user" do insert(:admin, email: "admin@example.com", - password_hash: Bcrypt.hash_pwd_salt("admin") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("admin") ) insert(:user, @@ -107,7 +107,7 @@ defmodule Registrations.Integration.Messages do test "message sender name/address can be overridden" do insert(:admin, email: "admin@example.com", - password_hash: Bcrypt.hash_pwd_salt("admin") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("admin") ) navigate_to("/") @@ -139,7 +139,7 @@ defmodule Registrations.Integration.Messages do test "a message with show team enabled shows the actual team information instead of their details" do insert(:admin, email: "admin@example.com", - password_hash: Bcrypt.hash_pwd_salt("admin") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("admin") ) team = diff --git a/registrations/test/integration/teams_test.exs b/registrations/test/integration/teams_test.exs index d5a02c34..02d14ca2 100644 --- a/registrations/test/integration/teams_test.exs +++ b/registrations/test/integration/teams_test.exs @@ -38,7 +38,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) navigate_to("/") @@ -151,7 +151,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) navigate_to("/") @@ -168,7 +168,7 @@ defmodule Registrations.Integration.Teams do test "the table is hidden when empty" do insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) navigate_to("/") @@ -183,7 +183,7 @@ defmodule Registrations.Integration.Teams do insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) navigate_to("/") @@ -221,11 +221,14 @@ defmodule Registrations.Integration.Teams do takver = insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) bedap = - insert(:user, email: "bedap@example.com", password_hash: Bcrypt.hash_pwd_salt("Anarres")) + insert(:user, + email: "bedap@example.com", + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") + ) insert(:team, name: "A team", @@ -270,7 +273,7 @@ defmodule Registrations.Integration.UnmnemonicDevices.Teams do insert(:user, email: "takver@example.com", - password_hash: Bcrypt.hash_pwd_salt("Anarres") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Anarres") ) navigate_to("/") diff --git a/registrations/test/support/factory.ex b/registrations/test/support/factory.ex index 7b1ff6f9..b3a7c4a2 100644 --- a/registrations/test/support/factory.ex +++ b/registrations/test/support/factory.ex @@ -21,7 +21,7 @@ defmodule Registrations.Factory do user_factory(), %{ email: "octavia.butler@example.com", - password_hash: Bcrypt.hash_pwd_salt("Xenogenesis") + password_hash: Pow.Ecto.Schema.Password.pbkdf2_hash("Xenogenesis") } ) end From 4040003dab6236159b80fcb5afa93c2dfca58d14 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 18:45:42 -0500 Subject: [PATCH 17/37] Fix login redirection --- registrations/lib/registrations_web/plugs/login_required.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registrations/lib/registrations_web/plugs/login_required.ex b/registrations/lib/registrations_web/plugs/login_required.ex index 8c6412d0..97cfb678 100644 --- a/registrations/lib/registrations_web/plugs/login_required.ex +++ b/registrations/lib/registrations_web/plugs/login_required.ex @@ -15,7 +15,7 @@ defmodule RegistrationsWeb.Plugs.LoginRequired do conn |> Phoenix.Controller.put_flash(:info, "Please log in to edit your details") |> Phoenix.Controller.redirect( - to: RegistrationsWeb.Router.Helpers.session_path(conn, :new) + to: RegistrationsWeb.Router.Helpers.pow_session_path(conn, :new) ) |> halt end From 07cdb538999026b3db053a1cea988d76fb35b63d Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 19:29:27 -0500 Subject: [PATCH 18/37] Fix some flash message assertions --- .../test/integration/registrations_test.exs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/registrations/test/integration/registrations_test.exs b/registrations/test/integration/registrations_test.exs index 96407232..84476e5f 100644 --- a/registrations/test/integration/registrations_test.exs +++ b/registrations/test/integration/registrations_test.exs @@ -28,14 +28,19 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Nav.register_link().click Register.submit() - assert Nav.error_text() == "Unable to create account" + + assert Nav.error_text() == + "Oops, something went wrong! Please check the errors below:\nPassword can't be blank\nEmail can't be blank" + assert Register.email_error() == "Email can't be blank" # FIXME fix plural detection assert Register.password_error() == "Password can't be blank" Register.fill_email("franklin.w.dixon@example.com") Register.submit() - assert Nav.error_text() == "Unable to create account" + + assert Nav.error_text() == + "Oops, something went wrong! Please check the errors below:\nPassword can't be blank" Register.fill_email("samuel.delaney@example.com") Register.fill_password("nestofspiders") @@ -71,7 +76,8 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Login.fill_password("Parable of the Talents") Login.submit() - assert Nav.error_text() == "Wrong email or password" + assert Nav.error_text() == + "The provided login details did not work. Please verify your credentials, and try again." Login.fill_password("Xenogenesis") Login.submit() @@ -127,7 +133,8 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Login.fill_password("Xenogenesis") Login.submit() - assert Nav.error_text() == "Wrong email or password" + assert Nav.error_text() == + "The provided login details did not work. Please verify your credentials, and try again." Login.fill_password("Lilith’s Brood") Login.submit() From 724237f4d6504d1b73a8b30bf77b086d5d85c41a Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 20:08:39 -0500 Subject: [PATCH 19/37] Fix various test failures --- registrations/config/config.exs | 3 ++- registrations/lib/registrations_web/pow_messages.ex | 8 ++++++++ .../templates/pow/registration/edit.html.eex | 6 +++--- .../templates/pow/registration/new.html.eex | 4 ++-- registrations/test/integration/registrations_test.exs | 11 +++++++---- registrations/test/support/pages/register.ex | 4 ++++ 6 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 registrations/lib/registrations_web/pow_messages.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 6e3f1ee4..6d6bbb01 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -38,7 +38,8 @@ config :registrations, :pow, web_module: RegistrationsWeb, user: RegistrationsWeb.User, repo: Registrations.Repo, - routes_backend: RegistrationsWeb.Pow.Routes + routes_backend: RegistrationsWeb.Pow.Routes, + messages_backend: RegistrationsWeb.Pow.Messages # Configure phoenix generators config :phoenix, :generators, diff --git a/registrations/lib/registrations_web/pow_messages.ex b/registrations/lib/registrations_web/pow_messages.ex new file mode 100644 index 00000000..f5f6c67c --- /dev/null +++ b/registrations/lib/registrations_web/pow_messages.ex @@ -0,0 +1,8 @@ +defmodule RegistrationsWeb.Pow.Messages do + use Pow.Phoenix.Messages + + def user_has_been_created(_conn), do: "Your account was created" + + def signed_in(_conn), do: "Logged in" + def signed_out(_conn), do: "Logged out" +end diff --git a/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex index 12295e2c..5436f405 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/edit.html.eex @@ -9,7 +9,7 @@
    <%= label f, :current_password %> - <%= password_input f, :current_password, id: 'current-password' %> + <%= password_input f, :current_password, id: 'current_password' %> <%= error_tag f, :current_password %>
    @@ -19,12 +19,12 @@
    <%= label f, :password %> - <%= password_input f, :password, id: 'password' %> + <%= password_input f, :password, id: 'new_password' %> <%= error_tag f, :password %>
    <%= label f, :password_confirmation %> - <%= password_input f, :password_confirmation, id: 'password-confirmation' %> + <%= password_input f, :password_confirmation, id: 'new_password_confirmation' %> <%= error_tag f, :password_confirmation %>
    diff --git a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex index 99039bef..ad6ab679 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex @@ -9,7 +9,7 @@
    <%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %> - <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %> + <%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset), id: 'email' %> <%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
    @@ -19,7 +19,7 @@
    <%= label f, :password_confirmation %> - <%= password_input f, :password_confirmation, id: 'password-confirmation' %> + <%= password_input f, :password_confirmation, id: 'password_confirmation' %> <%= error_tag f, :password_confirmation %>
    diff --git a/registrations/test/integration/registrations_test.exs b/registrations/test/integration/registrations_test.exs index 84476e5f..2b4cf884 100644 --- a/registrations/test/integration/registrations_test.exs +++ b/registrations/test/integration/registrations_test.exs @@ -44,6 +44,7 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Register.fill_email("samuel.delaney@example.com") Register.fill_password("nestofspiders") + Register.fill_password_confirmation("nestofspiders") Register.submit() assert Nav.info_text() == "Your account was created" @@ -108,21 +109,23 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Account.fill_current_password("Wrong") Account.submit() - assert Nav.error_text() == "Please enter your current password" + assert Nav.error_text() == + "Oops, something went wrong! Please check the errors below:\nCurrent password is invalid" Account.fill_current_password("Xenogenesis") Account.fill_new_password("abcde") Account.fill_new_password_confirmation("vwxyz") Account.submit() - assert Nav.error_text() == "New passwords must match" + assert Nav.error_text() == + "Oops, something went wrong! Please check the errors below:\nPassword should be at least 8 character(s)\nPassword confirmation does not match confirmation" Account.fill_current_password("Xenogenesis") Account.fill_new_password("Lilith’s Brood") Account.fill_new_password_confirmation("Lilith’s Brood") Account.submit() - assert Nav.info_text() == "Your password has been changed" + assert Nav.info_text() == "Your account has been updated." Nav.logout_link().click @@ -192,7 +195,7 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Account.fill_new_password_confirmation("anewpassword") Account.submit() - assert Nav.info_text() == "Your password has been changed" + assert Nav.info_text() == "Your account has been updated." assert Nav.logout_link().text == "Log out octavia.butler@example.com" Nav.logout_link().click diff --git a/registrations/test/support/pages/register.ex b/registrations/test/support/pages/register.ex index bc0e1f5c..77e4365f 100644 --- a/registrations/test/support/pages/register.ex +++ b/registrations/test/support/pages/register.ex @@ -13,6 +13,10 @@ defmodule Registrations.Pages.Register do fill_field({:id, "password"}, password) end + def fill_password_confirmation(password_confirmation) do + fill_field({:id, "password_confirmation"}, password_confirmation) + end + def password_error do visible_text({:css, ".errors .password"}) end From e36ed5998af36cdabcac35f9f64b912e238d723d Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 20:13:03 -0500 Subject: [PATCH 20/37] Move Pow overrides --- .../lib/registrations_web/{pow_messages.ex => pow/messages.ex} | 0 registrations/lib/registrations_web/{ => pow}/routes.ex | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename registrations/lib/registrations_web/{pow_messages.ex => pow/messages.ex} (100%) rename registrations/lib/registrations_web/{ => pow}/routes.ex (100%) diff --git a/registrations/lib/registrations_web/pow_messages.ex b/registrations/lib/registrations_web/pow/messages.ex similarity index 100% rename from registrations/lib/registrations_web/pow_messages.ex rename to registrations/lib/registrations_web/pow/messages.ex diff --git a/registrations/lib/registrations_web/routes.ex b/registrations/lib/registrations_web/pow/routes.ex similarity index 100% rename from registrations/lib/registrations_web/routes.ex rename to registrations/lib/registrations_web/pow/routes.ex From 35fac499471294d0781da370e2d217094e4dc14f Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 14 Jul 2024 20:21:02 -0500 Subject: [PATCH 21/37] Add preliminary mailer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This does nothing I guess…??? Without extensions --- registrations/config/config.exs | 1 + .../lib/registrations_web/pow/mailer.ex | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 registrations/lib/registrations_web/pow/mailer.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 6d6bbb01..cb96fce9 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -39,6 +39,7 @@ config :registrations, :pow, user: RegistrationsWeb.User, repo: Registrations.Repo, routes_backend: RegistrationsWeb.Pow.Routes, + mailer_backend: RegistrationsWeb.Pow.Mailer, messages_backend: RegistrationsWeb.Pow.Messages # Configure phoenix generators diff --git a/registrations/lib/registrations_web/pow/mailer.ex b/registrations/lib/registrations_web/pow/mailer.ex new file mode 100644 index 00000000..07b3cc91 --- /dev/null +++ b/registrations/lib/registrations_web/pow/mailer.ex @@ -0,0 +1,39 @@ +defmodule MyAppWeb.Pow.Mailer do + use Pow.Phoenix.Mailer + use Swoosh.Mailer, otp_app: :my_app + + import Swoosh.Email + + require Logger + + @impl true + def cast(%{user: user, subject: subject, text: text, html: html}) do + %Swoosh.Email{} + |> to({"", user.email}) + |> from({"My App", "myapp@example.com"}) + |> subject(subject) + |> html_body(html) + |> text_body(text) + end + + @impl true + def process(email) do + # An asynchronous process should be used here to prevent enumeration + # attacks. Synchronous e-mail delivery can reveal whether a user already + # exists in the system or not. + + Task.start(fn -> + email + |> deliver() + |> log_warnings() + end) + + :ok + end + + defp log_warnings({:error, reason}) do + Logger.warn("Mailer backend failed with: #{inspect(reason)}") + end + + defp log_warnings({:ok, response}), do: {:ok, response} +end From 3baf5a0cc74361cdc47817d0cc1244a6f47bbaf8 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 15 Jul 2024 17:49:46 -0500 Subject: [PATCH 22/37] Add email confirmation extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will require test fixes… gah --- registrations/config/config.exs | 4 +- registrations/lib/registrations/mailer.ex | 35 +++++++++++++++++ .../lib/registrations_web/models/user.ex | 8 ++++ .../lib/registrations_web/pow/mailer.ex | 39 ------------------- .../lib/registrations_web/pow/messages.ex | 1 + .../lib/registrations_web/pow/routes.ex | 1 + registrations/lib/registrations_web/router.ex | 2 + ...34_add_pow_email_confirmation_to_users.exs | 13 +++++++ 8 files changed, 63 insertions(+), 40 deletions(-) delete mode 100644 registrations/lib/registrations_web/pow/mailer.ex create mode 100644 registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs diff --git a/registrations/config/config.exs b/registrations/config/config.exs index cb96fce9..9ca9c83c 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -35,11 +35,13 @@ import_config "#{Mix.env()}.exs" config :phoenix, :json_library, Jason config :registrations, :pow, + extensions: [PowEmailConfirmation], + controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks, web_module: RegistrationsWeb, user: RegistrationsWeb.User, repo: Registrations.Repo, routes_backend: RegistrationsWeb.Pow.Routes, - mailer_backend: RegistrationsWeb.Pow.Mailer, + mailer_backend: Registrations.Mailer, messages_backend: RegistrationsWeb.Pow.Messages # Configure phoenix generators diff --git a/registrations/lib/registrations/mailer.ex b/registrations/lib/registrations/mailer.ex index 495781ab..81770708 100644 --- a/registrations/lib/registrations/mailer.ex +++ b/registrations/lib/registrations/mailer.ex @@ -1,13 +1,42 @@ defmodule Registrations.Mailer do + use Pow.Phoenix.Mailer use Swoosh.Mailer, otp_app: :registrations + import Swoosh.Email import RegistrationsWeb.SharedHelpers alias RegistrationsWeb.Router alias RegistrationsWeb.Endpoint + require Logger + @from "b@events.chromatin.ca" + @impl true + def cast(%{user: user, subject: subject, text: text, html: html}) do + %Swoosh.Email{} + |> to({"", user.email}) + |> from(adventure_from()) + |> subject(subject) + |> html_body(html) + |> text_body(text) + end + + @impl true + def process(email) do + # An asynchronous process should be used here to prevent enumeration + # attacks. Synchronous e-mail delivery can reveal whether a user already + # exists in the system or not. + + Task.start(fn -> + email + |> deliver() + |> log_warnings() + end) + + :ok + end + def send_welcome_email(email) do new() |> to(email) @@ -164,4 +193,10 @@ defmodule Registrations.Mailer do _ -> "From: #{message.from_name} <#{message.from_address}>" end end + + defp log_warnings({:error, reason}) do + Logger.warn("Mailer backend failed with: #{inspect(reason)}") + end + + defp log_warnings({:ok, response}), do: {:ok, response} end diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index 1e5b8dd0..5fc2864b 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -1,6 +1,8 @@ defmodule RegistrationsWeb.User do use Ecto.Schema + use Pow.Ecto.Schema + use Pow.Extension.Ecto.Schema, extensions: [PowEmailConfirmation] use RegistrationsWeb, :model alias Registrations.Repo @@ -31,6 +33,12 @@ defmodule RegistrationsWeb.User do timestamps() end + def changeset(user_or_changeset, attrs) do + user_or_changeset + |> pow_changeset(attrs) + |> pow_extension_changeset(attrs) + end + @required_fields ~w(email password)a @optional_fields ~w(team_emails proposed_team_name risk_aversion accessibility comments source team_id)a diff --git a/registrations/lib/registrations_web/pow/mailer.ex b/registrations/lib/registrations_web/pow/mailer.ex deleted file mode 100644 index 07b3cc91..00000000 --- a/registrations/lib/registrations_web/pow/mailer.ex +++ /dev/null @@ -1,39 +0,0 @@ -defmodule MyAppWeb.Pow.Mailer do - use Pow.Phoenix.Mailer - use Swoosh.Mailer, otp_app: :my_app - - import Swoosh.Email - - require Logger - - @impl true - def cast(%{user: user, subject: subject, text: text, html: html}) do - %Swoosh.Email{} - |> to({"", user.email}) - |> from({"My App", "myapp@example.com"}) - |> subject(subject) - |> html_body(html) - |> text_body(text) - end - - @impl true - def process(email) do - # An asynchronous process should be used here to prevent enumeration - # attacks. Synchronous e-mail delivery can reveal whether a user already - # exists in the system or not. - - Task.start(fn -> - email - |> deliver() - |> log_warnings() - end) - - :ok - end - - defp log_warnings({:error, reason}) do - Logger.warn("Mailer backend failed with: #{inspect(reason)}") - end - - defp log_warnings({:ok, response}), do: {:ok, response} -end diff --git a/registrations/lib/registrations_web/pow/messages.ex b/registrations/lib/registrations_web/pow/messages.ex index f5f6c67c..945bc23e 100644 --- a/registrations/lib/registrations_web/pow/messages.ex +++ b/registrations/lib/registrations_web/pow/messages.ex @@ -1,5 +1,6 @@ defmodule RegistrationsWeb.Pow.Messages do use Pow.Phoenix.Messages + use Pow.Extension.Phoenix.Messages, extensions: [PowEmailConfirmation] def user_has_been_created(_conn), do: "Your account was created" diff --git a/registrations/lib/registrations_web/pow/routes.ex b/registrations/lib/registrations_web/pow/routes.ex index a97a4e2b..01ffc065 100644 --- a/registrations/lib/registrations_web/pow/routes.ex +++ b/registrations/lib/registrations_web/pow/routes.ex @@ -1,6 +1,7 @@ defmodule RegistrationsWeb.Pow.Routes do use Pow.Phoenix.Routes + # FIXME how does confirmation affect this? @impl true def after_sign_in_path(conn), do: RegistrationsWeb.Router.Helpers.user_path(conn, :edit) end diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index c4b85120..d547c8c5 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -1,6 +1,7 @@ defmodule RegistrationsWeb.Router do use RegistrationsWeb, :router use Pow.Phoenix.Router + use Pow.Extension.Phoenix.Router, extensions: [PowEmailConfirmation] pipeline :browser do plug(:accepts, ["html"]) @@ -20,6 +21,7 @@ defmodule RegistrationsWeb.Router do pipe_through :browser pow_routes() + pow_extension_routes() end scope "/", RegistrationsWeb do diff --git a/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs b/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs new file mode 100644 index 00000000..a544d86a --- /dev/null +++ b/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs @@ -0,0 +1,13 @@ +defmodule Registrations.Repo.Migrations.AddPowEmailConfirmationToUsers do + use Ecto.Migration + + def change do + alter table(:users) do + add :email_confirmation_token, :string + add :email_confirmed_at, :utc_datetime + add :unconfirmed_email, :string + end + + create unique_index(:users, [:email_confirmation_token]) + end +end From c0cc28cb63076a00b1a1832da3ec995410e86400 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 15 Jul 2024 20:48:17 -0500 Subject: [PATCH 23/37] Add password reset --- registrations/config/config.exs | 3 ++- registrations/lib/registrations_web.ex | 9 +++++++ .../lib/registrations_web/models/user.ex | 2 +- .../lib/registrations_web/pow/messages.ex | 2 +- registrations/lib/registrations_web/router.ex | 5 +++- .../templates/pow/session/new.html.eex | 2 +- .../mailer/reset_password.html.eex | 4 ++++ .../mailer/reset_password.text.eex | 7 ++++++ .../reset_password/edit.html.eex | 24 +++++++++++++++++++ .../reset_password/new.html.eex | 18 ++++++++++++++ .../pow_email_confirmation/mailer_view.ex | 5 ++++ .../views/pow_reset_password/mailer_view.ex | 5 ++++ .../pow_reset_password/reset_password_view.ex | 3 +++ 13 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.html.eex create mode 100644 registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.text.eex create mode 100644 registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex create mode 100644 registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex create mode 100644 registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex create mode 100644 registrations/lib/registrations_web/views/pow_reset_password/mailer_view.ex create mode 100644 registrations/lib/registrations_web/views/pow_reset_password/reset_password_view.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 9ca9c83c..3a066012 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -35,7 +35,8 @@ import_config "#{Mix.env()}.exs" config :phoenix, :json_library, Jason config :registrations, :pow, - extensions: [PowEmailConfirmation], + web_mailer_module: RegistrationsWeb, + extensions: [PowEmailConfirmation, PowResetPassword], controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks, web_module: RegistrationsWeb, user: RegistrationsWeb.User, diff --git a/registrations/lib/registrations_web.ex b/registrations/lib/registrations_web.ex index 6a23fdd4..34f5e980 100644 --- a/registrations/lib/registrations_web.ex +++ b/registrations/lib/registrations_web.ex @@ -82,6 +82,15 @@ defmodule RegistrationsWeb do end end + def mailer_view do + quote do + use Phoenix.View, root: "lib/registrations_web/templates", + namespace: RegistrationsWeb + + use Phoenix.HTML + end + end + def router do quote do use Phoenix.Router diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index 5fc2864b..593865b2 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -2,7 +2,7 @@ defmodule RegistrationsWeb.User do use Ecto.Schema use Pow.Ecto.Schema - use Pow.Extension.Ecto.Schema, extensions: [PowEmailConfirmation] + use Pow.Extension.Ecto.Schema, extensions: [PowEmailConfirmation, PowResetPassword] use RegistrationsWeb, :model alias Registrations.Repo diff --git a/registrations/lib/registrations_web/pow/messages.ex b/registrations/lib/registrations_web/pow/messages.ex index 945bc23e..cb6d904f 100644 --- a/registrations/lib/registrations_web/pow/messages.ex +++ b/registrations/lib/registrations_web/pow/messages.ex @@ -1,6 +1,6 @@ defmodule RegistrationsWeb.Pow.Messages do use Pow.Phoenix.Messages - use Pow.Extension.Phoenix.Messages, extensions: [PowEmailConfirmation] + use Pow.Extension.Phoenix.Messages, extensions: [PowEmailConfirmation, PowResetPassword] def user_has_been_created(_conn), do: "Your account was created" diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index d547c8c5..6f36e2e4 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -1,7 +1,10 @@ defmodule RegistrationsWeb.Router do use RegistrationsWeb, :router use Pow.Phoenix.Router - use Pow.Extension.Phoenix.Router, extensions: [PowEmailConfirmation] + + use Pow.Extension.Phoenix.Router, + otp_app: :registrations, + extensions: [PowEmailConfirmation, PowResetPassword] pipeline :browser do plug(:accepts, ["html"]) diff --git a/registrations/lib/registrations_web/templates/pow/session/new.html.eex b/registrations/lib/registrations_web/templates/pow/session/new.html.eex index c3508ed0..691495c8 100644 --- a/registrations/lib/registrations_web/templates/pow/session/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/session/new.html.eex @@ -3,7 +3,7 @@

    Sign in

    - + <%= link "Forgot your password?", to: Routes.pow_reset_password_reset_password_path(@conn, :new), class: "forgot" %> <%= link "Register", to: Routes.pow_registration_path(@conn, :new) %> <%= form_for @changeset, @action, [as: :user], fn f -> %> <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.html.eex b/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.html.eex new file mode 100644 index 00000000..3af782a0 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.html.eex @@ -0,0 +1,4 @@ +<%= content_tag(:h3, "Hi,") %> +<%= content_tag(:p, "Please use the following link to reset your password:") %> +<%= content_tag(:p, link(@url, to: @url)) %> +<%= content_tag(:p, "You can disregard this email if you didn't request a password reset.") %> diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.text.eex b/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.text.eex new file mode 100644 index 00000000..8f90bf94 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_reset_password/mailer/reset_password.text.eex @@ -0,0 +1,7 @@ +Hi, + +Please use the following link to reset your password: + +<%= @url %> + +You can disregard this email if you didn't request a password reset. diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex new file mode 100644 index 00000000..ccbdc324 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex @@ -0,0 +1,24 @@ +

    Reset password

    + +<%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= if @changeset.action do %> +
    +

    Oops, something went wrong! Please check the errors below.

    +
    + <% end %> + + <%= label f, :password %> + <%= password_input f, :password %> + <%= error_tag f, :password %> + + <%= label f, :password_confirmation %> + <%= password_input f, :password_confirmation %> + <%= error_tag f, :password_confirmation %> + +
    + <%= submit "Submit" %> +
    +<% end %> + + +<%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %>%> diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex new file mode 100644 index 00000000..538690a6 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex @@ -0,0 +1,18 @@ +

    Reset password

    + +<%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= if @changeset.action do %> +
    +

    Oops, something went wrong! Please check the errors below.

    +
    + <% end %> + + <%= label f, :email %> + <%= text_input f, :email %> + <%= error_tag f, :email %> + +
    + <%= submit "Submit" %> +
    +<% end %> + diff --git a/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex b/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex new file mode 100644 index 00000000..4be3f266 --- /dev/null +++ b/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex @@ -0,0 +1,5 @@ +defmodule RegistrationsWeb.PowEmailConfirmation.MailerView do + use RegistrationsWeb, :mailer_view + + def subject(:email_confirmation, _assigns), do: "Confirm your email address" +end diff --git a/registrations/lib/registrations_web/views/pow_reset_password/mailer_view.ex b/registrations/lib/registrations_web/views/pow_reset_password/mailer_view.ex new file mode 100644 index 00000000..ae263571 --- /dev/null +++ b/registrations/lib/registrations_web/views/pow_reset_password/mailer_view.ex @@ -0,0 +1,5 @@ +defmodule RegistrationsWeb.PowResetPassword.MailerView do + use RegistrationsWeb, :mailer_view + + def subject(:reset_password, _assigns), do: "Reset password link" +end diff --git a/registrations/lib/registrations_web/views/pow_reset_password/reset_password_view.ex b/registrations/lib/registrations_web/views/pow_reset_password/reset_password_view.ex new file mode 100644 index 00000000..b3c19d96 --- /dev/null +++ b/registrations/lib/registrations_web/views/pow_reset_password/reset_password_view.ex @@ -0,0 +1,3 @@ +defmodule RegistrationsWeb.PowResetPassword.ResetPasswordView do + use RegistrationsWeb, :view +end From f81346a8acfcae64a48eba0acfd6ccb9c1207761 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 15 Jul 2024 20:49:56 -0500 Subject: [PATCH 24/37] Add confirmation templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why wouldn’t the generator work before and now it does? --- .../mailer/email_confirmation.html.eex | 3 +++ .../mailer/email_confirmation.text.eex | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex create mode 100644 registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex diff --git a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex new file mode 100644 index 00000000..f3102594 --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex @@ -0,0 +1,3 @@ +<%= content_tag(:h3, "Hi,") %> +<%= content_tag(:p, "Please use the following link to confirm your e-mail address:") %> +<%= content_tag(:p, link(@url, to: @url)) %> diff --git a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex new file mode 100644 index 00000000..0f311b4a --- /dev/null +++ b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex @@ -0,0 +1,5 @@ +Hi, + +Please use the following link to confirm your e-mail address: + +<%= @url %> From 17f5c7798b38762942471f795aab1750e89e3846 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 15 Jul 2024 20:57:46 -0500 Subject: [PATCH 25/37] Fix deprecation warning --- registrations/lib/registrations/mailer.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registrations/lib/registrations/mailer.ex b/registrations/lib/registrations/mailer.ex index 81770708..7fdf3d53 100644 --- a/registrations/lib/registrations/mailer.ex +++ b/registrations/lib/registrations/mailer.ex @@ -195,7 +195,7 @@ defmodule Registrations.Mailer do end defp log_warnings({:error, reason}) do - Logger.warn("Mailer backend failed with: #{inspect(reason)}") + Logger.warning("Mailer backend failed with: #{inspect(reason)}") end defp log_warnings({:ok, response}), do: {:ok, response} From 6237f224b27e6560404d1b53b9751abb5881f9bd Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 15 Jul 2024 23:29:16 -0500 Subject: [PATCH 26/37] Restore account deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pow doesn’t ask for the password… probably fine…??? --- registrations/config/config.exs | 3 ++- registrations/lib/registrations_web/pow/users.ex | 10 ++++++++++ .../registrations_web/templates/user/edit.html.heex | 2 +- registrations/test/integration/registrations_test.exs | 10 +--------- registrations/test/support/pages/details.ex | 1 + 5 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 registrations/lib/registrations_web/pow/users.ex diff --git a/registrations/config/config.exs b/registrations/config/config.exs index 3a066012..b64809d4 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -43,7 +43,8 @@ config :registrations, :pow, repo: Registrations.Repo, routes_backend: RegistrationsWeb.Pow.Routes, mailer_backend: Registrations.Mailer, - messages_backend: RegistrationsWeb.Pow.Messages + messages_backend: RegistrationsWeb.Pow.Messages, + users_context: RegistrationsWeb.Pow.Users # Configure phoenix generators config :phoenix, :generators, diff --git a/registrations/lib/registrations_web/pow/users.ex b/registrations/lib/registrations_web/pow/users.ex new file mode 100644 index 00000000..21963778 --- /dev/null +++ b/registrations/lib/registrations_web/pow/users.ex @@ -0,0 +1,10 @@ +defmodule RegistrationsWeb.Pow.Users do + use Pow.Ecto.Context, + repo: Registrations.Repo, + user: RegistrationsWeb.User + + def delete(params) do + pow_delete(params) + Registrations.Mailer.send_user_deletion(params) + end +end diff --git a/registrations/lib/registrations_web/templates/user/edit.html.heex b/registrations/lib/registrations_web/templates/user/edit.html.heex index 147a89ee..44896f56 100644 --- a/registrations/lib/registrations_web/templates/user/edit.html.heex +++ b/registrations/lib/registrations_web/templates/user/edit.html.heex @@ -101,6 +101,6 @@
    - + <%= link "Delete your account", to: Routes.pow_registration_path(@conn, :delete), method: :delete, class: "delete", data: [ confirm: "This will delete your account immediately, are you sure?" ] %>
    diff --git a/registrations/test/integration/registrations_test.exs b/registrations/test/integration/registrations_test.exs index 2b4cf884..dc48f38c 100644 --- a/registrations/test/integration/registrations_test.exs +++ b/registrations/test/integration/registrations_test.exs @@ -216,15 +216,7 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Nav.edit_details() Details.delete_account() - Account.fill_current_password("wrongpassword") - Account.submit() - - assert Nav.error_text() == "Your password did not match" - - Account.fill_current_password("Xenogenesis") - Account.submit() - - assert Nav.info_text() == "Your account has been deleted 😧" + assert Nav.info_text() == "Your account has been deleted. Sorry to see you go!" [admin_email] = Registrations.SwooshHelper.sent_email() diff --git a/registrations/test/support/pages/details.ex b/registrations/test/support/pages/details.ex index 76acfa87..458bf44f 100644 --- a/registrations/test/support/pages/details.ex +++ b/registrations/test/support/pages/details.ex @@ -7,6 +7,7 @@ defmodule Registrations.Pages.Details do def delete_account do click({:css, "a.delete"}) + accept_dialog() end def proposers do From 4ac74dc3cb2c8c9328bd3c9002a399e027c2aa0e Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 17:30:34 -0500 Subject: [PATCH 27/37] Remove outdated comment --- .../lib/registrations_web/templates/pow/session/new.html.eex | 1 - 1 file changed, 1 deletion(-) diff --git a/registrations/lib/registrations_web/templates/pow/session/new.html.eex b/registrations/lib/registrations_web/templates/pow/session/new.html.eex index 691495c8..d9d2e745 100644 --- a/registrations/lib/registrations_web/templates/pow/session/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/session/new.html.eex @@ -2,7 +2,6 @@

    Sign in

    - <%= link "Forgot your password?", to: Routes.pow_reset_password_reset_password_path(@conn, :new), class: "forgot" %> <%= link "Register", to: Routes.pow_registration_path(@conn, :new) %> <%= form_for @changeset, @action, [as: :user], fn f -> %> From 3652a11737d284784af2da7ac8ad96b79cb92d6f Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 18:07:14 -0500 Subject: [PATCH 28/37] Fix password reset test --- registrations/lib/registrations/mailer.ex | 2 +- .../reset_password/edit.html.eex | 39 ++++++++++--------- .../reset_password/new.html.eex | 29 +++++++------- .../test/integration/registrations_test.exs | 31 ++++++++------- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/registrations/lib/registrations/mailer.ex b/registrations/lib/registrations/mailer.ex index 7fdf3d53..c5b3bef3 100644 --- a/registrations/lib/registrations/mailer.ex +++ b/registrations/lib/registrations/mailer.ex @@ -17,7 +17,7 @@ defmodule Registrations.Mailer do %Swoosh.Email{} |> to({"", user.email}) |> from(adventure_from()) - |> subject(subject) + |> subject("[#{phrase("email_title")}] #{subject}") |> html_body(html) |> text_body(text) end diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex index ccbdc324..7a192e1b 100644 --- a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex +++ b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/edit.html.eex @@ -1,24 +1,25 @@ -

    Reset password

    +
    +
    +

    Reset password

    -<%= form_for @changeset, @action, [as: :user], fn f -> %> - <%= if @changeset.action do %> -
    -

    Oops, something went wrong! Please check the errors below.

    -
    - <% end %> + <%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - <%= label f, :password %> - <%= password_input f, :password %> - <%= error_tag f, :password %> +
    + <%= label f, :password %> + <%= password_input f, :password, id: 'new_password' %> + <%= error_tag f, :password %> +
    - <%= label f, :password_confirmation %> - <%= password_input f, :password_confirmation %> - <%= error_tag f, :password_confirmation %> +
    + <%= label f, :password_confirmation %> + <%= password_input f, :password_confirmation, id: 'new_password_confirmation' %> + <%= error_tag f, :password_confirmation %> +
    -
    - <%= submit "Submit" %> +
    + <%= submit "Submit", class: "button primary" %> +
    + <% end %>
    -<% end %> - - -<%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %>%> +
    diff --git a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex index 538690a6..cbf878e2 100644 --- a/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow_reset_password/reset_password/new.html.eex @@ -1,18 +1,19 @@ -

    Reset password

    +
    +
    +

    Reset password

    -<%= form_for @changeset, @action, [as: :user], fn f -> %> - <%= if @changeset.action do %> -
    -

    Oops, something went wrong! Please check the errors below.

    -
    - <% end %> + <%= form_for @changeset, @action, [as: :user], fn f -> %> + <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - <%= label f, :email %> - <%= text_input f, :email %> - <%= error_tag f, :email %> +
    + <%= label f, :email %> + <%= text_input f, :email, id: :email %> + <%= error_tag f, :email %> +
    -
    - <%= submit "Submit" %> +
    + <%= submit "Submit", class: "button primary" %> +
    + <% end %>
    -<% end %> - +
    diff --git a/registrations/test/integration/registrations_test.exs b/registrations/test/integration/registrations_test.exs index dc48f38c..b0eb5ecc 100644 --- a/registrations/test/integration/registrations_test.exs +++ b/registrations/test/integration/registrations_test.exs @@ -155,22 +155,17 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Nav.login_link().click Login.click_forgot_password() - ForgotPassword.fill_email("noone@example.com") - ForgotPassword.submit() - - assert Nav.error_text() == "No registration with that email address found" - refute Registrations.SwooshHelper.emails_sent?() - ForgotPassword.fill_email("octavia.butler@example.com") ForgotPassword.submit() - assert Nav.info_text() == "Check your email for a password reset link" + assert Nav.info_text() == + "If an account for the provided email exists, an email with reset instructions will be sent to you. Please check your inbox." [forgot_password_email] = Registrations.SwooshHelper.sent_email() assert forgot_password_email.to == [{"", "octavia.butler@example.com"}] assert forgot_password_email.from == {"", "b@events.chromatin.ca"} - assert forgot_password_email.subject == "[rendezvous] Password reset" + assert forgot_password_email.subject == "[rendezvous] Reset password link" [url] = Floki.find(forgot_password_email.html_body, "a") @@ -178,10 +173,10 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do reset_path = URI.parse(url).path - assert String.starts_with?(reset_path, "/reset/%242b") + assert String.starts_with?(reset_path, "/reset-password/") - navigate_to("/reset/fake") - assert Nav.error_text() == "Unknown password reset token" + navigate_to("/reset-password/fake") + assert Nav.error_text() == "The reset token has expired." navigate_to(reset_path) @@ -189,13 +184,19 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Account.fill_new_password_confirmation("awrongpassword") Account.submit() - assert Nav.error_text() == "New passwords must match" + assert Nav.error_text() == + "Oops, something went wrong! Please check the errors below:\nPassword confirmation does not match confirmation" Account.fill_new_password("anewpassword") Account.fill_new_password_confirmation("anewpassword") Account.submit() - assert Nav.info_text() == "Your account has been updated." + assert Nav.info_text() == "The password has been updated." + + Login.fill_email("Octavia.butler@example.com") + Login.fill_password("anewpassword") + Login.submit() + assert Nav.logout_link().text == "Log out octavia.butler@example.com" Nav.logout_link().click @@ -203,8 +204,10 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do Login.login_as("octavia.butler@example.com", "anewpassword") assert Nav.logout_link().text == "Log out octavia.butler@example.com" + Nav.logout_link().click + navigate_to(reset_path) - assert Nav.error_text() == "Unknown password reset token" + assert Nav.error_text() == "The reset token has expired." end test "delete account" do From 6bb50a145113894e6cc7b87063207bc0bfd1f640 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 18:57:48 -0500 Subject: [PATCH 29/37] Restore email downcasing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don’t love having to remove the macro but oh well! --- registrations/lib/registrations_web/models/user.ex | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index 593865b2..cca10b72 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -10,7 +10,16 @@ defmodule RegistrationsWeb.User do @primary_key {:id, :binary_id, autogenerate: true} schema "users" do - pow_user_fields() + # pow_user_fields() with overridden email field + field(:email, RegistrationsWeb.DowncasedString) + field(:password_hash, :string) + field(:current_password, :string, virtual: true) + field(:password, :string, virtual: true) + field(:confirm_password, :string, virtual: true) + + field(:email_confirmation_token, :string) + field(:email_confirmed_at, :utc_datetime) + field(:unconfirmed_email, :string) field(:admin, :boolean) From 2eb169f00dcace82f3089f8bef018ad84e0a91d9 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 19:31:45 -0500 Subject: [PATCH 30/37] Remove reset code --- registrations/lib/registrations/mailer.ex | 16 ----- .../controllers/reset_controller.ex | 66 ------------------- 2 files changed, 82 deletions(-) delete mode 100644 registrations/lib/registrations_web/controllers/reset_controller.ex diff --git a/registrations/lib/registrations/mailer.ex b/registrations/lib/registrations/mailer.ex index c5b3bef3..01044f2d 100644 --- a/registrations/lib/registrations/mailer.ex +++ b/registrations/lib/registrations/mailer.ex @@ -5,9 +5,6 @@ defmodule Registrations.Mailer do import Swoosh.Email import RegistrationsWeb.SharedHelpers - alias RegistrationsWeb.Router - alias RegistrationsWeb.Endpoint - require Logger @from "b@events.chromatin.ca" @@ -111,19 +108,6 @@ defmodule Registrations.Mailer do |> deliver end - @spec send_password_reset(atom | %{:email => any, :recovery_hash => any, optional(any) => any}) :: - {:error, any} | {:ok, any} - def send_password_reset(user) do - new() - |> to(user.email) - |> from(adventure_from()) - |> subject("[#{phrase("email_title")}] Password reset") - |> html_body( - "Here is a password reset link" - ) - |> deliver - end - defp welcome_html do Phoenix.View.render_to_string( RegistrationsWeb.EmailView, diff --git a/registrations/lib/registrations_web/controllers/reset_controller.ex b/registrations/lib/registrations_web/controllers/reset_controller.ex deleted file mode 100644 index e54a32f1..00000000 --- a/registrations/lib/registrations_web/controllers/reset_controller.ex +++ /dev/null @@ -1,66 +0,0 @@ -defmodule RegistrationsWeb.ResetController do - use RegistrationsWeb, :controller - alias RegistrationsWeb.User - alias Registrations.Repo - - def new(conn, _params) do - changeset = User.reset_changeset(%User{}) - render(conn, changeset: changeset) - end - - def create(conn, %{"user" => user_params}) do - user = Repo.get_by(RegistrationsWeb.User, email: user_params["email"]) - - case RegistrationsWeb.Reset.create(user, Repo) do - {:ok, user} -> - Registrations.Mailer.send_password_reset(user) - - conn - |> put_flash(:info, "Check your email for a password reset link") - |> redirect(to: Routes.page_path(conn, :index)) - - {:error, _} -> - conn - |> put_flash(:error, "No registration with that email address found") - |> render("new.html", changeset: User.reset_changeset(%User{})) - end - end - - def edit(conn, %{"token" => token}) do - case Repo.get_by(RegistrationsWeb.User, recovery_hash: token) do - nil -> - conn - |> put_flash(:error, "Unknown password reset token") - |> redirect(to: Routes.page_path(conn, :index)) - - user -> - changeset = User.perform_reset_changeset(user, %{"recovery_hash" => token}) - render(conn, changeset: changeset, token: token) - end - end - - def update(conn, %{"user" => user_params}) do - user = - Repo.get_by(RegistrationsWeb.User, recovery_hash: user_params["recovery_hash"]) || - %User{} - - changeset = User.perform_reset_changeset(user, user_params) - - case RegistrationsWeb.Reset.update(changeset, Repo) do - {:ok, user} -> - conn - |> put_session(:current_user, user.id) - |> put_flash(:info, "Your password has been changed") - |> redirect(to: Routes.user_path(conn, :edit)) - - {:error, changeset} -> - # TODO this is a hack to ensure the token is present in the hidden field when an attempt fails, but why isn’t it already? - changeset = - Ecto.Changeset.put_change(changeset, :recovery_hash, user_params["recovery_hash"]) - - conn - |> put_flash(:error, "New passwords must match") - |> render("edit.html", changeset: changeset, token: user_params["recovery_hash"]) - end - end -end From 9bfeba772c35cf197147793eb332c4b047095352 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 19:48:48 -0500 Subject: [PATCH 31/37] Fix loading of user when editing --- .../lib/registrations_web/controllers/user_controller.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/registrations/lib/registrations_web/controllers/user_controller.ex b/registrations/lib/registrations_web/controllers/user_controller.ex index 7663ca50..7b6f3cf4 100644 --- a/registrations/lib/registrations_web/controllers/user_controller.ex +++ b/registrations/lib/registrations_web/controllers/user_controller.ex @@ -27,7 +27,7 @@ defmodule RegistrationsWeb.UserController do def edit(conn, _) do users = Repo.all(User) - current_user_only = conn.assigns[:current_user] + current_user_only = Repo.get_by(User, email: conn.assigns[:current_user].email) changeset = User.details_changeset(current_user_only) current_user = Repo.preload(current_user_only, team: [:users]) From d8487dfed9855b35b455426bd8158ffb8c0121ca Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 21:21:46 -0500 Subject: [PATCH 32/37] Remove confirmation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Is it possible to hook into confirmation to send email? I never had confirmation before so I’m just removing it. --- registrations/config/config.exs | 2 +- registrations/lib/registrations_web/models/user.ex | 6 +----- registrations/lib/registrations_web/pow/messages.ex | 2 +- registrations/lib/registrations_web/router.ex | 2 +- .../mailer/email_confirmation.html.eex | 3 --- .../mailer/email_confirmation.text.eex | 5 ----- .../views/pow_email_confirmation/mailer_view.ex | 5 ----- ...15012334_add_pow_email_confirmation_to_users.exs | 13 ------------- unmnemonic_devices_vrs/tests/fixtures/schema.sql | 5 +++-- waydowntown/waydowntown_server/db/schema.rb | 6 +++--- 10 files changed, 10 insertions(+), 39 deletions(-) delete mode 100644 registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex delete mode 100644 registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex delete mode 100644 registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex delete mode 100644 registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs diff --git a/registrations/config/config.exs b/registrations/config/config.exs index b64809d4..3293977f 100644 --- a/registrations/config/config.exs +++ b/registrations/config/config.exs @@ -36,7 +36,7 @@ config :phoenix, :json_library, Jason config :registrations, :pow, web_mailer_module: RegistrationsWeb, - extensions: [PowEmailConfirmation, PowResetPassword], + extensions: [PowResetPassword], controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks, web_module: RegistrationsWeb, user: RegistrationsWeb.User, diff --git a/registrations/lib/registrations_web/models/user.ex b/registrations/lib/registrations_web/models/user.ex index cca10b72..0dd68661 100644 --- a/registrations/lib/registrations_web/models/user.ex +++ b/registrations/lib/registrations_web/models/user.ex @@ -2,7 +2,7 @@ defmodule RegistrationsWeb.User do use Ecto.Schema use Pow.Ecto.Schema - use Pow.Extension.Ecto.Schema, extensions: [PowEmailConfirmation, PowResetPassword] + use Pow.Extension.Ecto.Schema, extensions: [PowResetPassword] use RegistrationsWeb, :model alias Registrations.Repo @@ -17,10 +17,6 @@ defmodule RegistrationsWeb.User do field(:password, :string, virtual: true) field(:confirm_password, :string, virtual: true) - field(:email_confirmation_token, :string) - field(:email_confirmed_at, :utc_datetime) - field(:unconfirmed_email, :string) - field(:admin, :boolean) field(:attending, :boolean) diff --git a/registrations/lib/registrations_web/pow/messages.ex b/registrations/lib/registrations_web/pow/messages.ex index cb6d904f..ce9c39fa 100644 --- a/registrations/lib/registrations_web/pow/messages.ex +++ b/registrations/lib/registrations_web/pow/messages.ex @@ -1,6 +1,6 @@ defmodule RegistrationsWeb.Pow.Messages do use Pow.Phoenix.Messages - use Pow.Extension.Phoenix.Messages, extensions: [PowEmailConfirmation, PowResetPassword] + use Pow.Extension.Phoenix.Messages, extensions: [PowResetPassword] def user_has_been_created(_conn), do: "Your account was created" diff --git a/registrations/lib/registrations_web/router.ex b/registrations/lib/registrations_web/router.ex index 6f36e2e4..3e42b2cf 100644 --- a/registrations/lib/registrations_web/router.ex +++ b/registrations/lib/registrations_web/router.ex @@ -4,7 +4,7 @@ defmodule RegistrationsWeb.Router do use Pow.Extension.Phoenix.Router, otp_app: :registrations, - extensions: [PowEmailConfirmation, PowResetPassword] + extensions: [PowResetPassword] pipeline :browser do plug(:accepts, ["html"]) diff --git a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex deleted file mode 100644 index f3102594..00000000 --- a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.html.eex +++ /dev/null @@ -1,3 +0,0 @@ -<%= content_tag(:h3, "Hi,") %> -<%= content_tag(:p, "Please use the following link to confirm your e-mail address:") %> -<%= content_tag(:p, link(@url, to: @url)) %> diff --git a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex b/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex deleted file mode 100644 index 0f311b4a..00000000 --- a/registrations/lib/registrations_web/templates/pow_email_confirmation/mailer/email_confirmation.text.eex +++ /dev/null @@ -1,5 +0,0 @@ -Hi, - -Please use the following link to confirm your e-mail address: - -<%= @url %> diff --git a/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex b/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex deleted file mode 100644 index 4be3f266..00000000 --- a/registrations/lib/registrations_web/views/pow_email_confirmation/mailer_view.ex +++ /dev/null @@ -1,5 +0,0 @@ -defmodule RegistrationsWeb.PowEmailConfirmation.MailerView do - use RegistrationsWeb, :mailer_view - - def subject(:email_confirmation, _assigns), do: "Confirm your email address" -end diff --git a/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs b/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs deleted file mode 100644 index a544d86a..00000000 --- a/registrations/priv/repo/migrations/20240715012334_add_pow_email_confirmation_to_users.exs +++ /dev/null @@ -1,13 +0,0 @@ -defmodule Registrations.Repo.Migrations.AddPowEmailConfirmationToUsers do - use Ecto.Migration - - def change do - alter table(:users) do - add :email_confirmation_token, :string - add :email_confirmed_at, :utc_datetime - add :unconfirmed_email, :string - end - - create unique_index(:users, [:email_confirmation_token]) - end -end diff --git a/unmnemonic_devices_vrs/tests/fixtures/schema.sql b/unmnemonic_devices_vrs/tests/fixtures/schema.sql index b9f3fb55..b1d0ac20 100644 --- a/unmnemonic_devices_vrs/tests/fixtures/schema.sql +++ b/unmnemonic_devices_vrs/tests/fixtures/schema.sql @@ -111,8 +111,8 @@ END) STORED CREATE TABLE public.users ( id uuid NOT NULL, - email character varying(255), - crypted_password character varying(255), + email character varying(255) NOT NULL, + password_hash character varying(255), inserted_at timestamp(0) without time zone NOT NULL, updated_at timestamp(0) without time zone NOT NULL, admin boolean, @@ -633,3 +633,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20240630162710); INSERT INTO public."schema_migrations" (version) VALUES (20240630162715); INSERT INTO public."schema_migrations" (version) VALUES (20240703014400); INSERT INTO public."schema_migrations" (version) VALUES (20240703235731); +INSERT INTO public."schema_migrations" (version) VALUES (20240714173901); diff --git a/waydowntown/waydowntown_server/db/schema.rb b/waydowntown/waydowntown_server/db/schema.rb index 72ce2dd7..6cadd491 100644 --- a/waydowntown/waydowntown_server/db/schema.rb +++ b/waydowntown/waydowntown_server/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 20_240_703_235_731) do +ActiveRecord::Schema[7.1].define(version: 20_240_714_173_901) do create_schema 'unmnemonic_devices' create_schema 'waydowntown' @@ -68,8 +68,8 @@ end create_table 'users', id: :uuid, default: nil, force: :cascade do |t| - t.string 'email', limit: 255 - t.string 'crypted_password', limit: 255 + t.string 'email', limit: 255, null: false + t.string 'password_hash', limit: 255 t.datetime 'inserted_at', precision: 0, null: false t.datetime 'updated_at', precision: 0, null: false t.boolean 'admin' From 8009a54a2b92b479782240e18af6ee0fc8cea9e9 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 22:10:52 -0500 Subject: [PATCH 33/37] Fix sending of backlog --- registrations/lib/registrations/mailer.ex | 2 ++ .../lib/registrations_web/pow/users.ex | 27 +++++++++++++++++++ .../test/integration/messages_test.exs | 3 ++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/registrations/lib/registrations/mailer.ex b/registrations/lib/registrations/mailer.ex index 01044f2d..6ab0b73c 100644 --- a/registrations/lib/registrations/mailer.ex +++ b/registrations/lib/registrations/mailer.ex @@ -73,6 +73,8 @@ defmodule Registrations.Mailer do |> deliver end + @spec send_registration(atom() | %{:email => any(), optional(any()) => any()}) :: + {:error, any()} | {:ok, any()} def send_registration(user) do new() |> to(adventure_from()) diff --git a/registrations/lib/registrations_web/pow/users.ex b/registrations/lib/registrations_web/pow/users.ex index 21963778..db68dd04 100644 --- a/registrations/lib/registrations_web/pow/users.ex +++ b/registrations/lib/registrations_web/pow/users.ex @@ -3,6 +3,33 @@ defmodule RegistrationsWeb.Pow.Users do repo: Registrations.Repo, user: RegistrationsWeb.User + import Ecto.Query, only: [from: 2] + + def create(params) do + case pow_create(params) do + {:ok, user} -> + messages = + Registrations.Repo.all( + from(m in RegistrationsWeb.Message, + where: m.ready == true, + select: m, + order_by: :postmarked_at + ) + ) + + unless Enum.empty?(messages) do + Registrations.Mailer.send_backlog(messages, user) + end + + Registrations.Mailer.send_welcome_email(user.email) + Registrations.Mailer.send_registration(user) + {:ok, user} + + {:error, changeset} -> + {:error, changeset} + end + end + def delete(params) do pow_delete(params) Registrations.Mailer.send_user_deletion(params) diff --git a/registrations/test/integration/messages_test.exs b/registrations/test/integration/messages_test.exs index 6f184b49..eb74b5b6 100644 --- a/registrations/test/integration/messages_test.exs +++ b/registrations/test/integration/messages_test.exs @@ -209,9 +209,10 @@ defmodule Registrations.Integration.Messages do Register.fill_email("registerer@example.com") Register.fill_password("abcdefghi") + Register.fill_password_confirmation("abcdefghi") Register.submit() - [backlog_email, _welcome_, _admin] = Registrations.SwooshHelper.sent_email() + [_admin, _welcome, backlog_email] = Registrations.SwooshHelper.sent_email() assert backlog_email.to == [{"", "registerer@example.com"}] assert backlog_email.from == {"", "b@events.chromatin.ca"} From 6030d67f592c8ccc98f08efacbcd5bf3e7453404 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 22:19:36 -0500 Subject: [PATCH 34/37] Fix more tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’ve had so many problems over the years with the order of emails in these lists, does it really matter? Maybe a helper would be good to extract by title or sender. --- registrations/test/integration/registrations_test.exs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/registrations/test/integration/registrations_test.exs b/registrations/test/integration/registrations_test.exs index b0eb5ecc..b010eb7c 100644 --- a/registrations/test/integration/registrations_test.exs +++ b/registrations/test/integration/registrations_test.exs @@ -49,7 +49,7 @@ defmodule Registrations.Integration.ClandestineRendezvous.Registrations do assert Nav.info_text() == "Your account was created" - [welcome_email, admin_email] = Registrations.SwooshHelper.sent_email() + [admin_email, welcome_email] = Registrations.SwooshHelper.sent_email() assert admin_email.to == [{"", "b@events.chromatin.ca"}] assert admin_email.from == {"", "b@events.chromatin.ca"} @@ -273,9 +273,10 @@ defmodule Registrations.Integration.UnmnemonicDevices.Registrations do Register.fill_email("samuel.delaney@example.com") Register.fill_password("nestofspiders") + Register.fill_password_confirmation("nestofspiders") Register.submit() - [welcome_email, admin_email] = Registrations.SwooshHelper.sent_email() + [admin_email, welcome_email] = Registrations.SwooshHelper.sent_email() assert admin_email.to == [{"", "knut@chromatin.ca"}] assert admin_email.from == {"", "knut@chromatin.ca"} From 440486d5566f86ae1eed23a6134aab15502990cc Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 23:08:17 -0500 Subject: [PATCH 35/37] Restore warning when registration closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don’t love it here but it works! --- .../templates/pow/registration/new.html.eex | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex index ad6ab679..f0cfada5 100644 --- a/registrations/lib/registrations_web/templates/pow/registration/new.html.eex +++ b/registrations/lib/registrations_web/templates/pow/registration/new.html.eex @@ -1,3 +1,12 @@ +<%# This used to be in the controller but I couldn’t find a hook for it %> +<%= if Application.get_env(:registrations, :registration_closed) do %> +
    +
    + +
    +
    +<% end %> +

    Register

    From f1f8d73258694886509096a54acaa29fe996137d Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Tue, 16 Jul 2024 23:30:29 -0500 Subject: [PATCH 36/37] Add user reload after voicepass save --- registrations/lib/registrations_web.ex | 2 ++ .../lib/registrations_web/controllers/user_controller.ex | 3 ++- registrations/lib/registrations_web/pow/controller_helper.ex | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 registrations/lib/registrations_web/pow/controller_helper.ex diff --git a/registrations/lib/registrations_web.ex b/registrations/lib/registrations_web.ex index 34f5e980..f755e89f 100644 --- a/registrations/lib/registrations_web.ex +++ b/registrations/lib/registrations_web.ex @@ -35,6 +35,8 @@ defmodule RegistrationsWeb do import Ecto.Query, only: [from: 1, from: 2] alias RegistrationsWeb.Router.Helpers, as: Routes + + import RegistrationsWeb.Pow.ControllerHelper end end diff --git a/registrations/lib/registrations_web/controllers/user_controller.ex b/registrations/lib/registrations_web/controllers/user_controller.ex index 7b6f3cf4..5cd530ce 100644 --- a/registrations/lib/registrations_web/controllers/user_controller.ex +++ b/registrations/lib/registrations_web/controllers/user_controller.ex @@ -87,7 +87,8 @@ defmodule RegistrationsWeb.UserController do changeset = User.voicepass_changeset(current_user, %{voicepass: new_voicepass}) - Repo.update(changeset) + {:ok, new_user} = Repo.update(changeset) + conn = sync_user(conn, new_user) json(conn, %{data: %{voicepass: new_voicepass}}) end diff --git a/registrations/lib/registrations_web/pow/controller_helper.ex b/registrations/lib/registrations_web/pow/controller_helper.ex new file mode 100644 index 00000000..3f256355 --- /dev/null +++ b/registrations/lib/registrations_web/pow/controller_helper.ex @@ -0,0 +1,4 @@ +defmodule RegistrationsWeb.Pow.ControllerHelper do + @spec sync_user(Plug.Conn.t(), map()) :: Plug.Conn.t() + def sync_user(conn, user), do: Pow.Plug.create(conn, user) +end From e0bd99043994630aec21bf31c6dbe080fc0cc1bc Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Wed, 17 Jul 2024 17:39:34 -0500 Subject: [PATCH 37/37] Remove superseded templates --- .../templates/registration/edit.html.heex | 32 ------------------- .../registration/maybe_delete.html.heex | 22 ------------- .../templates/registration/new.html.heex | 27 ---------------- .../templates/reset/edit.html.heex | 29 ----------------- .../templates/reset/new.html.heex | 22 ------------- .../templates/session/new.html.heex | 23 ------------- 6 files changed, 155 deletions(-) delete mode 100644 registrations/lib/registrations_web/templates/registration/edit.html.heex delete mode 100644 registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex delete mode 100644 registrations/lib/registrations_web/templates/registration/new.html.heex delete mode 100644 registrations/lib/registrations_web/templates/reset/edit.html.heex delete mode 100644 registrations/lib/registrations_web/templates/reset/new.html.heex delete mode 100644 registrations/lib/registrations_web/templates/session/new.html.heex diff --git a/registrations/lib/registrations_web/templates/registration/edit.html.heex b/registrations/lib/registrations_web/templates/registration/edit.html.heex deleted file mode 100644 index 5a7b5463..00000000 --- a/registrations/lib/registrations_web/templates/registration/edit.html.heex +++ /dev/null @@ -1,32 +0,0 @@ -
    -
    -

    Change Password

    - - <%= form_for @changeset, Routes.pow_registration_path(@conn, :update), fn f -> %> - <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - -
    -
    -
    - -
    -
    - -
    -
    - -
    -
    - <%= submit "Change password", class: "button primary" %> -
    -
    -
    - <% end %> -
    -
    diff --git a/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex b/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex deleted file mode 100644 index 806ebb8a..00000000 --- a/registrations/lib/registrations_web/templates/registration/maybe_delete.html.heex +++ /dev/null @@ -1,22 +0,0 @@ -
    -
    -

    Delete account

    - - <%= form_for @changeset, Routes.pow_registration_path(@conn, :delete), fn f -> %> - <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - -
    -
    -
    - -
    -
    - <%= submit "Delete account", class: "button primary" %> -
    -
    -
    - <% end %> -
    -
    diff --git a/registrations/lib/registrations_web/templates/registration/new.html.heex b/registrations/lib/registrations_web/templates/registration/new.html.heex deleted file mode 100644 index 385a51df..00000000 --- a/registrations/lib/registrations_web/templates/registration/new.html.heex +++ /dev/null @@ -1,27 +0,0 @@ -
    -
    -

    Register

    - - <%= form_for @changeset, Routes.pow_registration_path(@conn, :create), fn f -> %> - <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - -
    -
    -
    - -
    -
    - -
    -
    - <%= submit "Signup", class: "button primary" %> -
    -
    -
    - <% end %> -
    -
    diff --git a/registrations/lib/registrations_web/templates/reset/edit.html.heex b/registrations/lib/registrations_web/templates/reset/edit.html.heex deleted file mode 100644 index 36f47918..00000000 --- a/registrations/lib/registrations_web/templates/reset/edit.html.heex +++ /dev/null @@ -1,29 +0,0 @@ -
    -
    -

    Set Password

    - - <%= form_for @changeset, Routes.reset_path(@conn, :update, @token), fn f -> %> - <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - - <%= hidden_input f, :recovery_hash %> - -
    -
    -
    - -
    -
    - -
    -
    - <%= submit "Set Password", class: "button primary" %> -
    -
    -
    - <% end %> -
    -
    diff --git a/registrations/lib/registrations_web/templates/reset/new.html.heex b/registrations/lib/registrations_web/templates/reset/new.html.heex deleted file mode 100644 index da5eb800..00000000 --- a/registrations/lib/registrations_web/templates/reset/new.html.heex +++ /dev/null @@ -1,22 +0,0 @@ -
    -
    -

    Reset Password

    - - <%= form_for @changeset, Routes.reset_path(@conn, :create), fn f -> %> - <%= render RegistrationsWeb.SharedView, "errors.html", changeset: @changeset, f: f %> - -
    -
    -
    - -
    -
    - <%= submit "Reset password", class: "button primary" %> -
    -
    -
    - <% end %> -
    -
    diff --git a/registrations/lib/registrations_web/templates/session/new.html.heex b/registrations/lib/registrations_web/templates/session/new.html.heex deleted file mode 100644 index 528c5599..00000000 --- a/registrations/lib/registrations_web/templates/session/new.html.heex +++ /dev/null @@ -1,23 +0,0 @@ -
    -
    -

    Login

    - - <%= link "Forgot your password?", to: Routes.reset_path(@conn, :new), class: "forgot" %> - - <%= form_for @conn, Routes.session_path(@conn, :create), [as: :session], fn f -> %> -
    - - <%= email_input f, :email, id: "email", class: "form-control" %> -
    - -
    - - <%= password_input f, :password, id: "password", class: "form-control" %> -
    - -
    - <%= submit "Login", class: "button primary" %> -
    - <% end %> -
    -