1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/lib/chess/auth/user.ex

38 lines
813 B
Elixir

defmodule Chess.Auth.User do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
alias Comeonin.Argon2
schema "users" do
field :password, :string, virtual: true
field :password_hash, :string
field :username, :string
has_many :games, Chess.Store.Game
has_many :games_as_opponent, Chess.Store.Game, foreign_key: :opponent_id
timestamps()
end
@doc false
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:username, :password])
|> validate_required([:username, :password])
|> unique_constraint(:username)
|> hash_password()
end
defp hash_password(changeset) do
password = get_change(changeset, :password)
if password do
changeset
|> change(Argon2.add_hash(password))
else
changeset
end
end
end