Skip to content

Instantly share code, notes, and snippets.

@aaronrenner
Created January 8, 2016 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronrenner/c94fd4508c8bd8a2484e to your computer and use it in GitHub Desktop.
Save aaronrenner/c94fd4508c8bd8a2484e to your computer and use it in GitHub Desktop.
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