diff --git a/lib/factory.ex b/lib/factory.ex index 4355d78..7af0fe7 100644 --- a/lib/factory.ex +++ b/lib/factory.ex @@ -28,7 +28,7 @@ defmodule LaunchCart.Factory do %User{ email: Internet.email(), hashed_password: Bcrypt.hash_pwd_salt("password"), - active?: true + confirmed_at: DateTime.utc_now() } end diff --git a/lib/launch_cart/accounts.ex b/lib/launch_cart/accounts.ex index ebddeaf..10ea1bf 100644 --- a/lib/launch_cart/accounts.ex +++ b/lib/launch_cart/accounts.ex @@ -40,7 +40,7 @@ defmodule LaunchCart.Accounts do """ def get_user_by_email_and_password(email, password) when is_binary(email) and is_binary(password) do - user = Repo.get_by(User, email: email, active?: true) + user = Repo.get_by(User, email: email) if User.valid_password?(user, password), do: user end diff --git a/lib/launch_cart/accounts/user.ex b/lib/launch_cart/accounts/user.ex index 1613f49..6fb3293 100644 --- a/lib/launch_cart/accounts/user.ex +++ b/lib/launch_cart/accounts/user.ex @@ -34,8 +34,10 @@ defmodule LaunchCart.Accounts.User do """ def registration_changeset(user, attrs, opts \\ []) do user - |> cast(attrs, [:email, :notes]) + |> cast(attrs, [:email, :notes, :password]) |> validate_email() + |> validate_confirmation(:password, message: "does not match password") + |> validate_password(opts) end defp validate_email(changeset) do diff --git a/lib/launch_cart/accounts/user_notifier.ex b/lib/launch_cart/accounts/user_notifier.ex index 341c854..5d32c11 100644 --- a/lib/launch_cart/accounts/user_notifier.ex +++ b/lib/launch_cart/accounts/user_notifier.ex @@ -23,17 +23,15 @@ defmodule LaunchCart.Accounts.UserNotifier do def deliver_confirmation_instructions(user, url) do deliver(user.email, "Confirmation instructions", """ - ============================== - Hi #{user.email}, - You can confirm your account by visiting the URL below: + Thanks for signing for Launch Elements! You can confirm your account by visiting the URL below: #{url} If you didn't create an account with us, please ignore this. - ============================== + """) end @@ -43,8 +41,6 @@ defmodule LaunchCart.Accounts.UserNotifier do def deliver_reset_password_instructions(user, url) do deliver(user.email, "Reset password instructions", """ - ============================== - Hi #{user.email}, You can reset your password by visiting the URL below: @@ -52,8 +48,6 @@ defmodule LaunchCart.Accounts.UserNotifier do #{url} If you didn't request this change, please ignore this. - - ============================== """) end diff --git a/lib/launch_cart_web/controllers/user_registration_controller.ex b/lib/launch_cart_web/controllers/user_registration_controller.ex index 1619edd..095a936 100644 --- a/lib/launch_cart_web/controllers/user_registration_controller.ex +++ b/lib/launch_cart_web/controllers/user_registration_controller.ex @@ -3,7 +3,6 @@ defmodule LaunchCartWeb.UserRegistrationController do alias LaunchCart.Accounts alias LaunchCart.Accounts.User - alias LaunchCartWeb.UserAuth def new(conn, _params) do changeset = Accounts.change_user_registration(%User{}) @@ -11,9 +10,14 @@ defmodule LaunchCartWeb.UserRegistrationController do end def create(conn, %{"user" => user_params}) do - case Accounts.register_user(user_params) do - {:ok, user} -> render(conn, "thanks.html", user: user) - + with {:ok, user} <- Accounts.register_user(user_params), + {:ok, _} <- + Accounts.deliver_user_confirmation_instructions( + user, + &Routes.user_confirmation_url(conn, :edit, &1) + ) do + render(conn, "thanks.html", user: user) + else {:error, %Ecto.Changeset{} = changeset} -> render(conn, "new.html", changeset: changeset) end diff --git a/lib/launch_cart_web/controllers/user_session_controller.ex b/lib/launch_cart_web/controllers/user_session_controller.ex index fdd491d..eae2251 100644 --- a/lib/launch_cart_web/controllers/user_session_controller.ex +++ b/lib/launch_cart_web/controllers/user_session_controller.ex @@ -2,6 +2,7 @@ defmodule LaunchCartWeb.UserSessionController do use LaunchCartWeb, :controller alias LaunchCart.Accounts + alias LaunchCart.Accounts.User alias LaunchCartWeb.UserAuth def new(conn, _params) do @@ -11,11 +12,18 @@ defmodule LaunchCartWeb.UserSessionController do def create(conn, %{"user" => user_params}) do %{"email" => email, "password" => password} = user_params - if user = Accounts.get_user_by_email_and_password(email, password) do - UserAuth.log_in_user(conn, user, user_params) - else - # In order to prevent user enumeration attacks, don't disclose whether the email is registered. - render(conn, "new.html", error_message: "Invalid email or password") + case Accounts.get_user_by_email_and_password(email, password) do + %User{confirmed_at: nil} -> + render(conn, "new.html", + error_message: "Please check your email for confirmation instructions." + ) + + %User{} = user -> + UserAuth.log_in_user(conn, user, user_params) + + nil -> + # In order to prevent user enumeration attacks, don't disclose whether the email is registered + render(conn, "new.html", error_message: "Invalid email or password") end end diff --git a/lib/launch_cart_web/templates/user_registration/new.html.heex b/lib/launch_cart_web/templates/user_registration/new.html.heex index 7d3bb4d..5218ed2 100644 --- a/lib/launch_cart_web/templates/user_registration/new.html.heex +++ b/lib/launch_cart_web/templates/user_registration/new.html.heex @@ -1,7 +1,7 @@
We are currently in beta and looking for folks to help us test out our product. Interested? - Enter your email address and some info on what you are hoping to build, and we'll get right back to you!
+We are currently in open beta testing from now until the end of November, 2023. Getting started is easy! + Fill out the form below, we'll confirm your email and you are on your way.
<.form let={f} for={@changeset} action={Routes.user_registration_path(@conn, :create)}> <%= if @changeset.action do %> @@ -17,7 +17,19 @@Thank you for your interest in testing our product. We know that getting real user feedback is the best way to build the right thing, so we can't thank you enough. We'll be in touch soon.
+You should receive an email shortly. Follow the confirmation instructions and you are ready to start using Launch Elements!
Back to homepage
- Don't have an account yet? <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> for one now!
+ Don't have an account yet? <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> for one now!
+ Need to confirm your email? <%= link "Resend confirmation instructions", to: Routes.user_confirmation_path(@conn, :new) %>