mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-18 15:08:00 +01:00
43 lines
1.2 KiB
Elixir
43 lines
1.2 KiB
Elixir
|
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
|