mobilizon/lib/eventos/accounts/user.ex
2017-12-09 14:58:37 +01:00

47 lines
1 KiB
Elixir

defmodule Eventos.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Accounts.{User}
schema "users" do
field :email, :string
field :role, :integer, default: 0
field :password, :string, virtual: true
field :password_hash, :string
field :account_id, :integer
timestamps()
end
@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:email, :password_hash, :role])
|> validate_required([:email])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
end
def registration_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, ~w(password)a, [])
|> validate_length(:password, min: 6, max: 100)
|> hash_password
end
defp hash_password(changeset) do
case changeset do
%Ecto.Changeset{valid?: true,
changes: %{password: password}} ->
put_change(changeset,
:password_hash,
Comeonin.Argon2.hashpwsalt(password))
_ ->
changeset
end
end
end