Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule PhoenixApi.User do
- use PhoenixApi.Web, :model
- schema "users" do
- field :name, :string
- field :email, :string
- field :password_hash, :string
- field :password, :string, virtual: true
- timestamps()
- end
- def changeset(struct, params \\ %{}) do
- struct
- |> cast(params, [:name, :email])
- |> validate_length(:email, min: 6, max: 255)
- |> validate_required([:name, :email])
- |> validate_format(:email, ~r/\A[^@\s]+@[^@\s]+\z/)
- |> unique_constraint(:email)
- end
- def signup_changeset(struct, params \\ %{}) do
- struct
- |> changeset(params)
- |> cast(params, [:password])
- |> validate_required([:password])
- |> validate_length(:password, min: 6, max: 20)
- |> encrypt_password
- end
- defp encrypt_password(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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement