Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. defmodule PhoenixApi.User do
  2. use PhoenixApi.Web, :model
  3.  
  4. schema "users" do
  5. field :name, :string
  6. field :email, :string
  7. field :password_hash, :string
  8. field :password, :string, virtual: true
  9.  
  10. timestamps()
  11. end
  12.  
  13. def changeset(struct, params \\ %{}) do
  14. struct
  15. |> cast(params, [:name, :email])
  16. |> validate_length(:email, min: 6, max: 255)
  17. |> validate_required([:name, :email])
  18. |> validate_format(:email, ~r/\A[^@\s]+@[^@\s]+\z/)
  19. |> unique_constraint(:email)
  20. end
  21.  
  22. def signup_changeset(struct, params \\ %{}) do
  23. struct
  24. |> changeset(params)
  25. |> cast(params, [:password])
  26. |> validate_required([:password])
  27. |> validate_length(:password, min: 6, max: 20)
  28. |> encrypt_password
  29. end
  30.  
  31. defp encrypt_password(changeset) do
  32. case changeset do
  33. %Ecto.Changeset{valid?: true, changes: %{password: password}} ->
  34. put_change(changeset, :password_hash, Comeonin.Bcrypt.hashpwsalt(password))
  35. _ ->
  36. changeset
  37. end
  38. end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement