View Source AshAuthentication.Phoenix.Controller behaviour (ash_authentication_phoenix v1.9.4)

The authentication controller generator.

Since authentication often requires explicit HTTP requests to do things like set cookies or return Authorization headers, use this module to create an AuthController in your Phoenix application.

Example

Handling the registration or authentication of a normal web-based user.

defmodule MyAppWeb.AuthController do
  use MyAppWeb, :controller
  use AshAuthentication.Phoenix.Controller

  def success(conn, _activity, user, _token) do
    conn
    |> store_in_session(user)
    |> assign(:current_user, user)
    |> redirect(to: Routes.page_path(conn, :index))
  end

  def failure(conn, _activity, _reason) do
    conn
    |> put_status(401)
    |> render("failure.html")
  end

  def sign_out(conn, _params) do
    conn
    |> clear_session()
    |> render("sign_out.html")
  end
end

Handling registration or authentication of an API user.

defmodule MyAppWeb.ApiAuthController do
  use MyAppWeb, :controller
  use AshAuthentication.Phoenix.Controller
  alias AshAuthentication.TokenRevocation

  def success(conn, _activity, _user, token) do
    conn
    |> put_status(200)
    |> json(%{
      authentication: %{
        status: :success,
        bearer: token}
    })
  end

  def failure(conn, _activity, _reason) do
    conn
    |> put_status(401)
    |> json(%{
      authentication: %{
        status: :failed
      }
    })
  end

  def sign_out(conn, _params) do
    conn
    |> revoke_bearer_tokens()
    |> json(%{
      status: :ok
    })
  end
end

Summary

Callbacks

Called when authentication fails.

Called when a request to sign out is received.

Called when authentication (or registration, depending on the provider) has been successful.

Types

@type activity() :: {strategy_name :: atom(), phase :: atom()}
@type t() :: module()
@type token() :: String.t() | nil
@type user() :: Ash.Resource.record() | nil

Callbacks

Link to this callback

failure(t, activity, reason)

View Source
@callback failure(Plug.Conn.t(), activity(), reason :: any()) :: Plug.Conn.t()

Called when authentication fails.

@callback sign_out(Plug.Conn.t(), params :: map()) :: Plug.Conn.t()

Called when a request to sign out is received.

Link to this callback

success(t, activity, user, token)

View Source
@callback success(Plug.Conn.t(), activity(), user(), token()) :: Plug.Conn.t()

Called when authentication (or registration, depending on the provider) has been successful.