Yes you can make any controller to overwrite a specific route and its action.
I have overwritten Pow registration controller with just a regular controller. For my understanding , i kind of added made it look alike. When you ran mix pow.phx.gen.templates, it creates a pow/registration/files inside lib/templates. I did the same added pow/registration_controller inside lib/controllers.
Entire Code Block
I created a module and named it pow/.
scope "/" do
pipe_through [:browser]
resources "/registration", MyAppWeb.Pow.RegistrationController, singleton: true, only: [:create]
end
controller:
defmodule MyAppWeb.Pow.RegistrationController do
use MyAppWeb, :controller
# def new(conn, _params) do
# # We'll leverage [`Pow.Plug`](Pow.Plug.html), but you can also follow the classic Phoenix way:
# # changeset = MyApp.Users.User.changeset(%MyApp.Users.User{}, %{})
# changeset = Pow.Plug.change_user(conn)
# render(conn, "new.html", changeset: changeset)
# end
def create(conn, %{"user" => user_params}) do
# We'll leverage [`Pow.Plug`](Pow.Plug.html), but you can also follow the classic Phoenix way:
# user =
# %MyApp.Users.User{}
# |> MyApp.Users.User.changeset(user_params)
# |> MyApp.Repo.insert()
case MyApp.Users.create_user_using_referral(user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "Welcome, Lets find your spouse!")
|> redirect(to: Routes.dashboard_path(conn, :index))
{:invalid_code, changeset} ->
conn
|> put_flash(:error, "You have entered an invalid code")
|> render("new.html", changeset: changeset, action: Routes.registration_path(conn, :create))
{:error, changeset} ->
conn
|> put_flash(:error, "Please correct the errors")
|> render("new.html", changeset: changeset, action: Routes.registration_path(conn, :create))
end
end
end
And i used pow custom context as well.
defmodule MyApp.Users do
use Pow.Ecto.Context,
repo: MyApp.Repo,
user: MyApp.Users.User
def create_user_using_referral(params) do
case params["referral_code"] do
nil -> pow_create(params)
"" -> pow_create(params)
code ->
case MyApp.Trial.get_by_referral_code(code) do
nil ->
changeset = User.changeset(%User{}, params)
Ecto.Changeset.add_error(changeset, :referral_code, "Entered code is invalid", validation: :invalid)
{:invalid_code, changeset}
referral_code ->
pow_create(params)
end
end
end
end