View Source PowInvitation.Ecto.Schema (Pow v1.0.38)

Handles the invitation schema for user.

Customize PowInvitation associations or fields

If you need to modify any of the associations or fields that PowInvitation adds to the user schema, you can override them by defining them before pow_user_fields/0:

defmodule MyApp.Users.User do
  use Ecto.Schema
  use Pow.Ecto.Schema
  use Pow.Extension.Ecto.Schema,
    extensions: [PowInvitation]

  schema "users" do
    belongs_to :invited_by, __MODULE__
    has_many :invited_users __MODULE__, foreign_key: :invited_by_id, on_delete: delete_all

    field :invitation_token, :string
    field :invitation_accepted_at, :utc_datetime

    pow_user_fields()

    timestamps()
  end
end

Customize PowInvitation changeset

You can extract individual changeset functions to modify the changeset flow entirely. As an example, this is how you can invite a user through email while using username as the user id field:

defmodule MyApp.Users.User do
  use Ecto.Schema
  use Pow.Ecto.Schema,
    user_id_field: :username

  import PowInvitation.Ecto.Schema,
    only: [invitation_token_changeset: 1, invited_by_changeset: 2]

  # ...

  def invite_changeset(user_or_changeset, invited_by, attrs) do
    user_or_changeset
    |> cast(attrs, [:email])
    |> validate_required([:email])
    |> invitation_token_changeset()
    |> invited_by_changeset(invited_by)
  end
end

Summary

Functions

Link to this function

accept_invitation_changeset(changeset, attrs)

View Source
@spec accept_invitation_changeset(Ecto.Schema.t() | Ecto.Changeset.t(), map()) ::
  Ecto.Changeset.t()

Accepts an invitation.

The changeset function in user schema module is called, and :invitation_accepted_at will be updated. The password can be set, and the user id updated.

Link to this function

invitation_token_changeset(changeset)

View Source
@spec invitation_token_changeset(Ecto.Schema.t() | Ecto.Changeset.t()) ::
  Ecto.Changeset.t()

Sets the invitation token.

Link to this function

invite_changeset(changeset, invited_by, attrs)

View Source
@spec invite_changeset(Ecto.Schema.t() | Ecto.Changeset.t(), Ecto.Schema.t(), map()) ::
  Ecto.Changeset.t()

Invites user.

It's important to note that this changeset should not include the changeset function in the user schema module if PowEmailConfirmation has been enabled. This is because the e-mail is assumed confirmed already if the user can accept the invitation.

A unique :invitation_token will be generated, and invited_by association will be set. Only the user id will be set, and the persisted user won't have any password for authentication.

Calls invitation_token_changeset/1 and invited_by_changeset/2.

Link to this function

invited_by_changeset(changeset, invited_by)

View Source
@spec invited_by_changeset(Ecto.Schema.t() | Ecto.Changeset.t(), Ecto.Schema.t()) ::
  Ecto.Changeset.t()

Sets the invited by association.