replace coherence with guardian
This commit is contained in:
@@ -4,6 +4,7 @@ defmodule Eventos.Accounts do
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
import Logger
|
||||
alias Eventos.Repo
|
||||
|
||||
alias Eventos.Accounts.User
|
||||
@@ -37,6 +38,32 @@ defmodule Eventos.Accounts do
|
||||
"""
|
||||
def get_user!(id), do: Repo.get!(User, id)
|
||||
|
||||
|
||||
@doc """
|
||||
Get an user by email
|
||||
"""
|
||||
def find(email) do
|
||||
Repo.get_by!(User, email: email)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Authenticate user
|
||||
"""
|
||||
def authenticate(%{user: user, password: password}) do
|
||||
# Does password match the one stored in the database?
|
||||
Logger.debug(user.password_hash)
|
||||
Logger.debug(password)
|
||||
case Comeonin.Argon2.checkpw(password, user.password_hash) do
|
||||
true ->
|
||||
# Yes, create and return the token
|
||||
EventosWeb.Guardian.encode_and_sign(user)
|
||||
_ ->
|
||||
# No, return an error
|
||||
{:error, :unauthorized}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@doc """
|
||||
Creates a user.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user