Pow v1.0.11 Pow.Ecto.Schema behaviour View Source

Handles the Ecto schema for user.

The macro will create a @pow_fields module attribute, and append fields to it using the attributes from Pow.Ecto.Schema.Fields.attrs/1. The pow_user_fields/0 macro will use these attributes to create fields in the ecto schema.

A default changeset/2 method is created, but can be overridden with a custom changeset/2 method.

Usage

Configure lib/my_project/users/user.ex the following way:

defmodule MyApp.Users.User do
  use Ecto.Schema
  use Pow.Ecto.Schema,
    user_id_field: :email,
    password_hash_methods: {&Pow.Ecto.Schema.Password.pbkdf2_hash/1,
                            &Pow.Ecto.Schema.Password.pbkdf2_verify/2},
    password_min_length: 10,
    password_max_length: 4096

  schema "users" do
    field :custom_field, :string

    pow_user_fields()

    timestamps()
  end

  def changeset(user_or_changeset, attrs) do
    pow_changeset(user, attrs)
  end
end

Remember to add user: MyApp.Users.User to your configuration.

Customize Pow fields

Pow fields can be overridden if the field name and type matches:

defmodule MyApp.Users.User do
  use Ecto.Schema
  use Pow.Ecto.Schema

  schema "users" do
    field :encrypted_password, :string
    field :password_hash, :string, source: :encrypted_password

    pow_user_fields()

    timestamps()
  end
end

Customize Pow changeset

You can extract individual changeset methods to modify the changeset flow entirely. As an example, this is how you can remove the validation check for confirm password in the changeset method:

defmodule MyApp.Users.User do
  # ...

  import Pow.Ecto.Schema.Changeset, only: [new_password_changeset: 3]

  # ...

  def changeset(user_or_changeset, attrs) do
    user_or_changeset
    |> pow_user_id_field_changeset(attrs)
    |> pow_current_password_changeset(attrs)
    |> new_password_changeset(attrs, @pow_config)
  end
end

Configuration options

  • :user_id_field - the field to use for user id. This value defaults to :email, and the changeset will automatically validate it as an e-mail.

Link to this section Summary

Functions

Normalizes the user id field.

A macro to add fields from the @pow_fields module attribute generated in __using__/1.

Get user id field key from changeset or configuration.

Link to this section Functions

Link to this function

filter_new_fields(fields, existing_fields) View Source

This function is deprecated. No longer public method.
Link to this function

normalize_user_id_field_value(value) View Source
normalize_user_id_field_value(binary()) :: binary()

Normalizes the user id field.

Keeps the user id field value case insensitive and removes leading and trailing whitespace.

Link to this macro

pow_user_fields() View Source (macro)

A macro to add fields from the @pow_fields module attribute generated in __using__/1.

The @pow_fields are populated by Pow.Ecto.Schema.Fields.attrs/1, and will have at minimum the following fields:

  • :email (if not changed with :user_id_field option)
  • :password_hash
  • :current_password (virtual)
  • :password (virtual)
  • :confirm_password (virtual)
Link to this function

user_id_field(config) View Source
user_id_field(Ecto.Changeset.t() | Pow.Config.t()) :: atom()

Get user id field key from changeset or configuration.

Defaults to :email.

Link to this section Callbacks

Link to this callback

verify_password(arg1, binary) View Source
verify_password(Ecto.Schema.t(), binary()) :: boolean()