Files
link_shortener/lib/link_shortener/accounts/user.ex
T
KKlochko cde8970b46
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build was killed
Add User model.
2023-08-05 21:15:40 +03:00

32 lines
729 B
Elixir

defmodule LinkShortener.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
field :email, :string
field :encrypted_password, :string
field :password, :string, virtual: true
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
|> unique_constraint(:email)
|> put_hashed_password()
end
defp put_hashed_password(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}}
->
put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password))
_ ->
changeset
end
end
end