2019-11-15 17:14:23 +01:00
|
|
|
defmodule PowCaptcha.Phoenix.ControllerCallbacks do
|
|
|
|
@moduledoc """
|
|
|
|
Controller callback logic for captcha verification.
|
|
|
|
"""
|
|
|
|
use Pow.Extension.Phoenix.ControllerCallbacks.Base
|
|
|
|
|
|
|
|
alias Pow.Config
|
|
|
|
alias Plug.Conn
|
|
|
|
alias Phoenix.Controller
|
|
|
|
|
|
|
|
alias Pow.Phoenix.RegistrationController
|
|
|
|
alias PowResetPassword.Phoenix.ResetPasswordController
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
@impl true
|
|
|
|
def before_process(RegistrationController, :create, conn, config) do
|
|
|
|
verifier = Config.get(config, :captcha_verifier)
|
|
|
|
return_path = routes(conn).registration_path(conn, :new)
|
|
|
|
|
|
|
|
verifier.valid_solution?(conn.params)
|
|
|
|
|> maybe_halt(conn, return_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_process(ResetPasswordController, :create, conn, config) do
|
|
|
|
verifier = Config.get(config, :captcha_verifier)
|
|
|
|
return_path = routes(conn).path_for(conn, ResetPasswordController, :new)
|
|
|
|
|
|
|
|
verifier.valid_solution?(conn.params)
|
|
|
|
|> maybe_halt(conn, return_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_halt(false, conn, return_path) do
|
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> Controller.put_flash(
|
|
|
|
:error,
|
|
|
|
"There was an error verifying you're not a robot. Please try again."
|
|
|
|
)
|
2019-11-15 17:14:23 +01:00
|
|
|
|> Controller.redirect(to: return_path)
|
|
|
|
|> Conn.halt()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_halt(true, conn, _return_path) do
|
|
|
|
conn
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|