Created
March 12, 2016 21:30
-
-
Save meinac/7cbe51dfb9b79441ff66 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Meinac.User do | |
use Meinac.Web, :model | |
schema "users" do | |
field :first_name, :string | |
field :last_name, :string | |
field :email, :string | |
field :password_digest, :string | |
field :password, :string, virtual: true | |
timestamps | |
end | |
@required_fields ~w(first_name last_name email password_digest) | |
@optional_fields ~w() | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, @required_fields, @optional_fields) | |
|> generate_password_digest | |
|> validate_format(:email, ~r/@/) | |
|> unique_constraint(:email) | |
end | |
defp generate_password_digest(changeset) do | |
case changeset.params["password"] do | |
nil -> changeset | |
password -> | |
changeset | |
|> put_change(:password_digest, Comeonin.Bcrypt.hashpwsalt(password)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example
as you see, it returns
blank
error for password_digest but it shouldn't.