defmodule <%= schema_module %> do use Ecto.Schema import Ecto.Changeset schema "<%= table %>" do field :email, :string field :password, :string, virtual: true, redact: true field :hashed_password, :string, redact: true field :confirmed_at, :naive_datetime timestamps(type: :utc_datetime) end def registration_changeset(user, attrs) do user |> cast(attrs, [:email, :password]) |> validate_email() |> validate_password() end def password_changeset(user, attrs) do user |> cast(attrs, [:password]) |> validate_password() end defp validate_email(changeset) do changeset |> validate_required([:email]) |> validate_format(:email, ~r/^[^\s]+@[^\s]+$/) |> unique_constraint(:email) end defp validate_password(changeset) do changeset |> validate_required([:password]) |> validate_length(:password, min: 12, max: 72) |> maybe_hash_password() end defp maybe_hash_password(changeset) do password = get_change(changeset, :password) if password do changeset |> put_change(:hashed_password, Dialup.Auth.Hash.hash_password(password)) |> delete_change(:password) else changeset end end end