I’m working on an app that uses Pow to handle user authentication. I was able to get it working by following the documentation, but I ran into an issue that I don’t see a clear solution for.
The application is for a subscription service where new users are given a free trial after signing up. I use Pow to create the user, which works fine. Now, I’m trying to associate the newly created user with a subscription using the controller callback feature from the Phoenix controllers section of the README. Here’s my implementation so far:
defmodule AddSubscription.Phoenix.ControllerCallbacks do
use Pow.Extension.Phoenix.ControllerCallbacks.Base
require Logger
@impl true
def before_respond(Pow.Phoenix.RegistrationController, :create, {:ok, user, conn}, _config) do
Logger.debug("user registration before_respond: #{inspect(user)}")
with {:ok, user} <- Trumpet.insert_subscription(%{user_id: user.id}) do
{:ok, user, conn}
end
end
end
I’ve also written a controller test to exercise the behavior works as intended:
defmodule Pow.Phoenix.RegistrationControllerTest do
use TrumpetWeb.ConnCase
describe "POST /registration" do
test "creates a user with a subscription", %{conn: conn} do
email = "wembley.gl@gmail.com"
post(
conn,
"/registration",
%{
user: %{
email: email,
password: "test1234567890",
confirm_password: "test1234567890",
}
}
)
user = Trumpet.get_user_by_email_with_subscription(email)
assert user.subscription
assert Trumpet.Subscription.trial?(user.subscription)
end
end
end
The user creation works as expected, but the controller callback isn’t firing, so the assertion for user.subscription
fails. I’ve taken a look at the Extensions section for Pow, from here, and looked at the implementation for other extensions, like PowPasswordReset, and it looks like an extension should be implemented as a library application, is that right? Also, how do I configure Pow to register an extension once it’s created? I apologize if this is in the documentation and I’m missing it.