Created
January 8, 2016 05:54
-
-
Save aaronrenner/c94fd4508c8bd8a2484e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Webcamery.User do | |
use Webcamery.Web, :model | |
schema "users" do | |
field :email, :string | |
field :password, :string, virtual: true | |
field :password_hash, :string | |
timestamps | |
end | |
@required_fields ~w(email) | |
@optional_fields ~w(password) | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
|> put_password_hash | |
end | |
defp put_password_hash(%Ecto.Changeset{valid?: true, changes: %{password: pass}} = changeset) do | |
put_change( | |
changeset, | |
:password_hash, | |
hashed_password(pass) | |
) | |
end | |
defp put_password_hash(changeset) do | |
changeset | |
end | |
defp hashed_password(password) do | |
Comeonin.Bcrypt.hashpwsalt(password) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment