I have a registration form, working perfectly.
The only problem I’m experiencing is that it’s allowing you to create multiple users with the same email.
What think it could be?
Controller User
defmodule MyappWeb.UserController do
#
plug Guardian.Plug.EnsureAuthenticated, [handler: UserController] when action in [:index]
#
def action(conn, _) do
apply(UserController, action_name(conn),
[conn, conn.params, current_user(conn)])
end
#
def create(conn, %{"user" => user_params}, user) do
case Registro.create_user(user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "Usuário #{user.nome} criado com sucesso.")
|> redirect(to: user_path(conn, :show, user))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset, user: user)
end
end
#
end
Model User
defmodule Myapp.Registro.User do # # def changeset(user, attrs) do user |> cast(attrs, [:nome, :email, :password, :plano_user, :status_user, :is_admin]) |> validate_required([:nome, :email]) |> validate_format(:email, ~r/@/, message: "E-mail incorreto!") |> validate_length(:password, min: 6, message: "Senha inferior a 6 dígitos") |> validate_confirmation(:password, message: "Senha não confere!") |> unique_constraint(:email) |> encrypt_password end # end
ecto create
defmodule Myapp.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :nome, :string
add :email, :string
add :hash_password, :string
add :plano_user, :string
add :status_user, :string, default: 1, null: false
add :is_admin, :boolean, default: false, null: false
timestamps()
end
end
end