Pow callback on logout or session expiry?

In the Pow GitHub issue 271, Chris McCord suggests broadcasting disconnect on logout or session expiry in order to disconnect active user connections:

Note that by default since a few versions ago that you can broadcast “disconnect” to the LV socket id to invalidate/kill any active user connections on logout, as long as you add the :live_socket_id to the session on login:


How could I hook into Pow to do that? Is there a way to get called back when the user logs out or the session expires? Is that what ControllerCallbacks are for?

Here is how I ended up solving it:

defmodule MyAppWeb.Pow.ControllerCallbacks do
  alias Pow.Extension.Phoenix.ControllerCallbacks
  alias Plug.Conn

  @live_socket_id_key :live_socket_id

  def before_respond(Pow.Phoenix.SessionController, :create, {:ok, conn}, config) do
    user = conn.assigns.current_user
    conn =
      |> Conn.put_session(:current_user_id, user.id)
      |> Conn.put_session(@live_socket_id_key, "users_sockets:#{user.id}")

    ControllerCallbacks.before_respond(Pow.Phoenix.SessionController, :create, {:ok, conn}, config)

  def before_respond(Pow.Phoenix.SessionController, :delete, {:ok, conn}, config) do
    live_socket_id = Conn.get_session(conn, @live_socket_id_key)
    BooklistiWeb.Endpoint.broadcast(live_socket_id, "disconnect", %{})

    ControllerCallbacks.before_respond(Pow.Phoenix.SessionController, :delete, {:ok, conn}, config)

  defdelegate before_respond(controller, action, results, config), to:  ControllerCallbacks

  defdelegate before_process(controller, action, results, config), to: ControllerCallbacks

Then, update config to point to the custom ControllerCallbacks:

config :myapp_web, :pow,
  controller_callbacks: MyAppWeb.Pow.ControllerCallbacks
1 Like

You shouldn’t really use controller callbacks for this, controller callbacks are meant for extensions.

I would recommend you to just override the delete function like this

In your router

defmodule MyAppWeb.Router do
  scope "/" do
    pipe_through [:browser]

    scope "/", MyAppWeb, as: "pow" do
      delete "/session", SessionController, :delete


and create a session controller with a delete function

defmodule MyAppWeb.SessionController do
  @moduledoc """
  Session controller based on https://github.com/danschultzer/pow/blob/master/lib/pow/phoenix/controllers/session_controller.ex
  import Pow.Phoenix.Controller, only: [require_authenticated: 2]
  use MyAppWeb, :controller
  alias Plug.Conn

  plug :require_authenticated when action in [:delete]

  @spec delete(Conn.t(), map()) :: Conn.t()
  def delete(conn, _params) do
    MyAppWeb.Endpoint.broadcast(get_session(conn, :live_socket_id), "disconnect", %{})

    |> Pow.Plug.delete()
    |> delete_session(:live_socket_id)
    |> redirect(to: Routes.pow_session_path(conn, :new))

Thanks, @Schultzer, appreciate it!