Skip to content

Instantly share code, notes, and snippets.

@conradwt
Created February 23, 2016 01:17
Show Gist options
  • Save conradwt/6277f9cc3fd1374ae0f1 to your computer and use it in GitHub Desktop.
Save conradwt/6277f9cc3fd1374ae0f1 to your computer and use it in GitHub Desktop.
Programming Phoenix
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