Skip to content

Instantly share code, notes, and snippets.

@bnhansn
Created October 20, 2016 20:17
Show Gist options
  • Save bnhansn/4dd47b6cc78fb49b43e24712d34f9ee0 to your computer and use it in GitHub Desktop.
Save bnhansn/4dd47b6cc78fb49b43e24712d34f9ee0 to your computer and use it in GitHub Desktop.
defmodule Sling.User do
use Sling.Web, :model
schema "users" do
field :username, :string
field :email, :string
field :password_hash, :string
field :password, :string, virtual: true
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:username, :email])
|> validate_required([:username, :email])
|> unique_constraint(:username)
|> unique_constraint(:email)
end
def registration_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, [:password])
|> validate_length(:password, min: 6, max: 100)
|> put_password_hash()
end
defp put_password_hash(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(password))
_ ->
changeset
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment