Created
February 23, 2016 01:17
-
-
Save conradwt/6277f9cc3fd1374ae0f1 to your computer and use it in GitHub Desktop.
Programming Phoenix
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 Rumbl.User do | |
use Rumbl.Web, :model | |
schema "users" do | |
field :name, :string | |
field :username, :string | |
field :password, :string, virtual: true | |
field :password_hash, :string | |
timestamps | |
end | |
def registration_changeset(model, params) do | |
model | |
|> changeset(params) | |
|> cast(params, ~w(password), []) | |
|> validate_length(:password, min: 6, max: 100) | |
|> put_pass_hash() | |
end | |
def changeset(model, params \\ :empty) do | |
model | |
|> cast(params, ~w(name username), []) | |
|> validate_length(:username, min: 1, max: 20) | |
end | |
defp put_pass_hash(changeset) do | |
case changeset do | |
%Ecto.Changeset{valid?: true, changes: %{password: pass}} -> | |
put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(pass)) | |
_ -> | |
changeset | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment