-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Friendly captcha #6546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ukutaht
wants to merge
11
commits into
master
Choose a base branch
from
friendly-captcha
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Friendly captcha #6546
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c72cca
feat: replace hCaptcha with Friendly Captcha
ukutaht 3f353e5
Use inivisible captcha
ukutaht d988793
feat: sync captcha widget theme with app light/dark mode
ukutaht fa1d811
refactor: extract shared Friendly Captcha widget component
ukutaht 5b162fd
refactor: inline captcha attribution into shared component
ukutaht 2cbec79
Merge branch 'master' into friendly-captcha
ukutaht d4bc5e7
Add moduledoc
ukutaht f62e808
Merge branch 'master' into friendly-captcha
ukutaht df118f5
Improve captcha styling
ukutaht f71599c
Add changelog entry
ukutaht 5084688
Self-host Friendly Captcha SDK
ukutaht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| defmodule PlausibleWeb.Components.Captcha do | ||
| @moduledoc """ | ||
| Friendly Captcha widget shared between the registration and password-reset forms. | ||
|
|
||
| Renders the (invisible) widget placeholder, the SDK script tags, and the reveal | ||
| script that: | ||
|
|
||
| * matches the widget to the app's resolved light/dark theme, | ||
| * reveals the widget only when the user must interact (or on error/slow solve), | ||
| * dispatches `frc-captcha-ready` / `frc-captcha-reset` window events so the | ||
| submit button can gate on a valid solution. | ||
|
|
||
| Pass `live?={true}` from a LiveView so the widget and scripts carry | ||
| `phx-update="ignore"` and survive DOM patching. | ||
| """ | ||
| use Phoenix.Component, global_prefixes: ~w(x-) | ||
|
|
||
| attr :live?, :boolean, default: false | ||
| attr :error, :string, default: nil | ||
|
|
||
| def widget(assigns) do | ||
| ~H""" | ||
| <div> | ||
| <div | ||
| phx-update={if @live?, do: "ignore"} | ||
| id="frc-captcha-placeholder" | ||
| class="frc-captcha hidden mb-2" | ||
| data-sitekey={PlausibleWeb.Captcha.sitekey()} | ||
| data-start="auto" | ||
| style="width: 100%" | ||
| > | ||
| </div> | ||
| <p :if={@error} class="text-xs text-red-500 mt-2"> | ||
| {@error} | ||
| </p> | ||
| <p class="text-xs text-gray-500 dark:text-gray-400"> | ||
| This site is protected by | ||
| <PlausibleWeb.Components.Generic.styled_link href="https://friendlycaptcha.com" new_tab={true}> | ||
| Friendly Captcha | ||
| </PlausibleWeb.Components.Generic.styled_link> | ||
| </p> | ||
| <script | ||
| phx-update={if @live?, do: "ignore"} | ||
| id="frc-captcha-script" | ||
| type="module" | ||
| src="https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@1/site.min.js" | ||
| async | ||
| defer | ||
| > | ||
| </script> | ||
| <script | ||
| phx-update={if @live?, do: "ignore"} | ||
| id="frc-captcha-script-compat" | ||
| nomodule | ||
| src="https://cdn.jsdelivr.net/npm/@friendlycaptcha/sdk@1/site.compat.min.js" | ||
| async | ||
| defer | ||
| > | ||
| </script> | ||
| <script phx-update={if @live?, do: "ignore"} id="frc-captcha-reveal"> | ||
| (function () { | ||
| var SHOW_AFTER_LONG_WAIT_MS = 5000; | ||
| var el = document.getElementById("frc-captcha-placeholder"); | ||
| if (!el) return; | ||
|
|
||
| // Match the widget to the app's resolved light/dark theme. This runs | ||
| // before the (deferred) SDK initializes, so the widget picks up the | ||
| // right theme from the start, and the observer keeps it in sync. | ||
| function applyTheme() { | ||
| el.dataset.theme = | ||
| document.documentElement.classList.contains("dark") ? "dark" : "light"; | ||
| } | ||
| applyTheme(); | ||
| new MutationObserver(applyTheme).observe(document.documentElement, { | ||
| attributes: true, | ||
| attributeFilter: ["class"] | ||
| }); | ||
|
|
||
| function show() { el.classList.remove("hidden"); } | ||
| var timeout; | ||
| // Friendly Captcha carries the event payload on `e.detail` (not `e`). | ||
| el.addEventListener("frc:widget.statechange", function (e) { | ||
| var d = e.detail || {}; | ||
| // Interactive mode means the user must click to solve: reveal the widget. | ||
| if (d.mode === "interactive") { show(); } | ||
| // Reveal if solving takes unusually long, then stop waiting once done. | ||
| if (d.state === "requesting") { | ||
| clearTimeout(timeout); | ||
| timeout = setTimeout(show, SHOW_AFTER_LONG_WAIT_MS); | ||
| } else if (d.state === "completed") { | ||
| clearTimeout(timeout); | ||
| } | ||
| // Reveal on error or expiry so the user can recover. | ||
| if (d.state === "error" || d.state === "expired") { show(); } | ||
| // Enable the submit button only once we hold a valid solution. | ||
| window.dispatchEvent(new Event( | ||
| d.state === "completed" ? "frc-captcha-ready" : "frc-captcha-reset" | ||
| )); | ||
| }); | ||
| })(); | ||
| </script> | ||
| </div> | ||
| """ | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.