diff --git a/assets/js/ujs.js b/assets/js/ujs.js index 12c8ba89..f869dfcb 100644 --- a/assets/js/ujs.js +++ b/assets/js/ujs.js @@ -2,7 +2,8 @@ import { $$, makeEl, findFirstTextNode } from './utils/dom'; import { fire, delegate, leftClick } from './utils/events'; const headers = () => ({ - 'x-csrf-token': window.booru.csrfToken + 'x-csrf-token': window.booru.csrfToken, + 'x-requested-with': 'XMLHttpRequest' }); function confirm(event, target) { @@ -56,9 +57,13 @@ function formRemote(event, target) { method: (target.dataset.method || target.method || 'POST').toUpperCase(), headers: headers(), body: new FormData(target) - }).then(response => + }).then(response => { + if (response && response.status == 300) { + window.location.reload(true); + return; + } fire(target, 'fetchcomplete', response) - ); + }); } function formReset(event, target) { diff --git a/lib/philomena_web/plugs/captcha_plug.ex b/lib/philomena_web/plugs/captcha_plug.ex index 2b5772fa..a43d8dc7 100644 --- a/lib/philomena_web/plugs/captcha_plug.ex +++ b/lib/philomena_web/plugs/captcha_plug.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.CaptchaPlug do alias Philomena.Captcha - alias Phoenix.Controller - alias Plug.Conn + import Plug.Conn + import Phoenix.Controller def init([]), do: false @@ -19,14 +19,31 @@ defmodule PhilomenaWeb.CaptchaPlug do false -> conn - |> Controller.put_flash( + |> put_flash( :error, "There was an error verifying you're not a robot. Please try again." ) - |> Controller.redirect(external: conn.assigns.referrer) - |> Conn.halt() + |> do_failure_response(ajax?(conn)) + |> halt() end end defp maybe_check_captcha(conn, _user), do: conn + + defp do_failure_response(conn, true) do + conn + |> put_status(:multiple_choices) + |> text("") + end + + defp do_failure_response(conn, _false) do + redirect(conn, external: conn.assigns.referrer) + end + + def ajax?(conn) do + case get_req_header(conn, "x-requested-with") do + [value] -> String.downcase(value) == "xmlhttprequest" + _ -> false + end + end end