philomena/lib/pow_captcha/phoenix/controllers/controller_callbacks.ex

43 lines
1.2 KiB
Elixir
Raw Normal View History

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
|> Controller.put_flash(:error, "There was an error verifying you're not a robot. Please try again.")
|> Controller.redirect(to: return_path)
|> Conn.halt()
end
defp maybe_halt(true, conn, _return_path) do
conn
end
end