authority v0.3.0 Authority.Registration behaviour

A behaviour for registering and updating users.

The core callbacks are:

  • create_user/1
  • get_user/1
  • update_user/2
  • delete_user/1

Applications which have a concept of changesets, (such as Ecto.Changeset for example) may find it useful to implement the optional callbacks as well:

  • change_user/0
  • change_user/1
  • change_user/2

Example

defmodule MyApp.Accounts.Registration do
  use Authority.Registration

  # OPTIONAL
  @impl Authority.Registration
  def change_user do
    # Return a changeset for a new user
  end

  # OPTIONAL
  @impl Authority.Registration
  def change_user(user, params \ %{}) do
    # Return a changeset for an existing user
  end

  # REQUIRED
  @impl Authority.Registration
  def create_user(params) do
    # Create a user
  end

  # REQUIRED
  @impl Authority.Registration
  def get_user(id) do
    # Get a user by ID
  end

  # REQUIRED
  @impl Authority.Registration
  def update_user(user, params) do
    # Update the user
  end

  # REQUIRED
  @impl Authority.Registration
  def delete_user(user) do
    # Delete the user
  end
end

Link to this section Summary

Types

Any type that represents a changeset

An error returned from creating/updating/deleting a user

An identifier for the user, such as an integer ID used as its primary key in the database

Parameters needed to create a user. For example,

A user. Can be any type that represents a user for your application

Callbacks

OPTIONAL. Returns a changeset for a new user

OPTIONAL. Returns a changeset for a given user

OPTIONAL. Returns a changeset for a given user and params

Creates a user

Deletes a user

Gets a user by ID

Updates a user with the given parameters

Link to this section Types

Link to this type changeset()
changeset() :: any()

Any type that represents a changeset.

Link to this type error()
error() :: {:error, term()}

An error returned from creating/updating/deleting a user.

Link to this type id()
id() :: any()

An identifier for the user, such as an integer ID used as its primary key in the database.

Link to this type params()
params() :: map()

Parameters needed to create a user. For example,

%{
  email: "my@email.com",
  password: "password",
  password_confirmation: "password"
}
Link to this type user()
user() :: any()

A user. Can be any type that represents a user for your application.

Link to this section Callbacks

Link to this callback change_user() (optional)
change_user() :: {:ok, changeset()} | error()

OPTIONAL. Returns a changeset for a new user.

Link to this callback change_user(user) (optional)
change_user(user()) :: {:ok, changeset()} | error()

OPTIONAL. Returns a changeset for a given user.

Link to this callback change_user(user, params) (optional)
change_user(user(), params :: map()) :: {:ok, changeset()} | error()

OPTIONAL. Returns a changeset for a given user and params.

Link to this callback create_user(params)
create_user(params()) :: {:ok, user()} | error()

Creates a user.

Link to this callback delete_user(user)
delete_user(user()) :: {:ok, user()} | error()

Deletes a user.

Link to this callback get_user(id)
get_user(id()) :: {:ok, user()} | error()

Gets a user by ID.

Link to this callback update_user(user, params)
update_user(user(), params()) :: {:ok, term()} | error()

Updates a user with the given parameters.