Extend custom controllers with additional logic
mainOnce you have custom controllers, you can inject additional business logic into the authentication flow. For example, you can check if a user's email is confirmed immediately after the authenticate_user step.
Best Practice: If you use custom controllers, avoid relying on the default controller actions provided by Pow extensions. Instead, implement the logic explicitly within your own controllers to keep the flow predictable.
defmodule MyAppWeb.SessionController do
# ...
def create(conn, %{"user" => user_params}) do
conn
|> Pow.Plug.authenticate_user(user_params)
|> verify_confirmed()
end
defp verify_confirmed({:ok, conn}) do
conn
|> Pow.Plug.current_user()
|> email_confirmed?()
|> case do
true ->
conn
|> put_flash(:info, "Welcome back!")
|> redirect(to: ~p"/")
false ->
conn
|> Pow.Plug.delete()
|> put_flash(:info, "Your e-mail address has not been confirmed.")
|> redirect(to: ~p"/login")
end
end
# ...
end