replace coherence with guardian

This commit is contained in:
Thomas Citharel
2017-12-09 14:58:37 +01:00
parent 90ceb4f6fe
commit 8ac705d8c2
52 changed files with 208 additions and 1254 deletions

View File

@@ -1,6 +1,5 @@
defmodule Eventos.Accounts.User do
use Ecto.Schema
use Coherence.Schema
import Ecto.Changeset
alias Eventos.Accounts.{User}
@@ -8,36 +7,40 @@ defmodule Eventos.Accounts.User do
schema "users" do
field :email, :string
field :role, :integer, default: 0
field :username, :string
field :password, :string, virtual: true
field :password_hash, :string
field :account_id, :integer
coherence_schema()
timestamps()
end
def changeset(user, attrs, :password) do
user
|> cast(attrs, ~w(password password_confirmation reset_password_token reset_password_sent_at))
|> validate_coherence_password_reset(attrs)
end
def changeset(user, attrs, :registration) do
user
|> cast(attrs, [:username, :email] ++ coherence_fields())
|> validate_required([:username, :email])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:username)
|> validate_coherence(attrs)
end
@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:username, :email, :password_hash, :role] ++ coherence_fields())
|> validate_required([:username, :email])
|> unique_constraint(:username)
|> cast(attrs, [:email, :password_hash, :role])
|> validate_required([:email])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
|> validate_coherence(attrs)
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