reject registration if user already exists
This commit is contained in:
parent
25abbf3f16
commit
f50d9916f4
|
@ -8,6 +8,10 @@ defmodule Chiya.Accounts do
|
|||
|
||||
alias Chiya.Accounts.{User, UserToken, UserNotifier}
|
||||
|
||||
def has_user?() do
|
||||
Repo.exists?(User)
|
||||
end
|
||||
|
||||
## Database getters
|
||||
|
||||
@doc """
|
||||
|
|
|
@ -31,6 +31,10 @@ defmodule ChiyaWeb.UserRegistrationLive do
|
|||
Oops, something went wrong! Please check the errors below.
|
||||
</.error>
|
||||
|
||||
<.error :if={@already_registered}>
|
||||
Oops, you cannot register. The bouncer has denied you entry!
|
||||
</.error>
|
||||
|
||||
<.input field={@form[:email]} type="email" label="Email" required />
|
||||
<.input field={@form[:password]} type="password" label="Password" required />
|
||||
|
||||
|
@ -47,26 +51,30 @@ defmodule ChiyaWeb.UserRegistrationLive do
|
|||
|
||||
socket =
|
||||
socket
|
||||
|> assign(trigger_submit: false, check_errors: false)
|
||||
|> assign(trigger_submit: false, check_errors: false, already_registered: false)
|
||||
|> assign_form(changeset)
|
||||
|
||||
{:ok, socket, temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"user" => user_params}, socket) do
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, user} ->
|
||||
{:ok, _} =
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
if Accounts.has_user?() do
|
||||
{:noreply, socket |> assign(already_registered: true)}
|
||||
else
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, user} ->
|
||||
{:ok, _} =
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
|
||||
changeset = Accounts.change_user_registration(user)
|
||||
{:noreply, socket |> assign(trigger_submit: true) |> assign_form(changeset)}
|
||||
changeset = Accounts.change_user_registration(user)
|
||||
{:noreply, socket |> assign(trigger_submit: true) |> assign_form(changeset)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, socket |> assign(check_errors: true) |> assign_form(changeset)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -54,20 +54,22 @@ defmodule ChiyaWeb.UserRegistrationLiveTest do
|
|||
assert response =~ "Settings"
|
||||
assert response =~ "Log out"
|
||||
end
|
||||
end
|
||||
|
||||
test "renders errors for duplicated email", %{conn: conn} do
|
||||
describe "register another user" do
|
||||
test "renders errors if another user already exists", %{conn: conn} do
|
||||
{:ok, lv, _html} = live(conn, ~p"/users/register")
|
||||
|
||||
user = user_fixture(%{email: "test@email.com"})
|
||||
_user = user_fixture(%{email: "test@email.com"})
|
||||
|
||||
result =
|
||||
lv
|
||||
|> form("#registration_form",
|
||||
user: %{"email" => user.email, "password" => "valid_password"}
|
||||
user: %{"email" => "attacker@email.com", "password" => "valid_password"}
|
||||
)
|
||||
|> render_submit()
|
||||
|
||||
assert result =~ "has already been taken"
|
||||
assert result =~ "you cannot register"
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue