philomena/lib/philomena_web/plugs/captcha_plug.ex

33 lines
699 B
Elixir
Raw Normal View History

2019-11-24 19:36:21 +01:00
defmodule PhilomenaWeb.CaptchaPlug do
alias Philomena.Captcha
alias Phoenix.Controller
alias Plug.Conn
def init([]), do: false
def call(conn, _opts) do
user = conn |> Pow.Plug.current_user()
conn
|> maybe_check_captcha(user)
end
defp maybe_check_captcha(conn, nil) do
case Captcha.valid_solution?(conn.params) do
2020-01-11 05:20:19 +01:00
true ->
conn
2019-11-24 19:36:21 +01:00
false ->
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-24 19:36:21 +01:00
|> Controller.redirect(external: conn.assigns.referrer)
|> Conn.halt()
end
end
2020-01-11 05:20:19 +01:00
2019-11-24 19:36:21 +01:00
defp maybe_check_captcha(conn, _user), do: conn
end