Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+33
-6
@@ -1,4 +1,6 @@
|
||||
import { createElement, useEffect, useRef } from "react";
|
||||
import type { Ref } from "react";
|
||||
import { getCurrentDocumentCspNonce } from "~/utils/security/documentCspNonce";
|
||||
|
||||
interface Props {
|
||||
/** Render the captcha widget. When false (or unconfigured) nothing is rendered. */
|
||||
@@ -9,6 +11,8 @@ interface Props {
|
||||
instanceUrl?: string;
|
||||
/** Public sitekey for this surface. */
|
||||
sitekey?: string;
|
||||
/** Reports whether a reusable form may submit a fresh proof. */
|
||||
onStatusChange?: (status: "pending" | "solved" | "error") => void;
|
||||
}
|
||||
|
||||
// mCaptcha's vanilla glue mounts the widget into an element with this id and
|
||||
@@ -23,13 +27,33 @@ const MOUNT_ID = "mcaptcha__widget-container";
|
||||
* hidden token field (`cap-token` or `mcaptcha__token`), so it is submitted
|
||||
* with the form and verified server-side by `@hivegrid/captcha`.
|
||||
*
|
||||
* Client load failures are non-fatal. The server verifier applies
|
||||
* CAPTCHA_FAIL_OPEN for provider outages, while a missing token remains a hard
|
||||
* failure when captcha is required.
|
||||
* Client load failures leave the form unsolved. The server verifier fails
|
||||
* closed in deployed runtimes; a local development/test process may explicitly
|
||||
* opt into fail-open behavior.
|
||||
*/
|
||||
export default function Captcha({ enabled, provider = "mcaptcha", instanceUrl, sitekey }: Props) {
|
||||
export default function Captcha({ enabled, provider = "mcaptcha", instanceUrl, sitekey, onStatusChange }: Props) {
|
||||
const active = enabled && Boolean(instanceUrl && sitekey);
|
||||
const initialized = useRef(false);
|
||||
const capWidgetRef = useRef<HTMLElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active) return;
|
||||
onStatusChange?.("pending");
|
||||
if (provider !== "cap") return;
|
||||
const widget = capWidgetRef.current;
|
||||
if (!widget) return;
|
||||
const solved = () => onStatusChange?.("solved");
|
||||
const reset = () => onStatusChange?.("pending");
|
||||
const failed = () => onStatusChange?.("error");
|
||||
widget.addEventListener("solve", solved);
|
||||
widget.addEventListener("reset", reset);
|
||||
widget.addEventListener("error", failed);
|
||||
return () => {
|
||||
widget.removeEventListener("solve", solved);
|
||||
widget.removeEventListener("reset", reset);
|
||||
widget.removeEventListener("error", failed);
|
||||
};
|
||||
}, [active, onStatusChange, provider]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active || initialized.current) return;
|
||||
@@ -62,15 +86,16 @@ export default function Captcha({ enabled, provider = "mcaptcha", instanceUrl, s
|
||||
Cap's custom element injects <input name="cap-token"> into the
|
||||
enclosing form when solved.
|
||||
*/}
|
||||
{createCapWidget(instanceUrl!, sitekey!)}
|
||||
{createCapWidget(instanceUrl!, sitekey!, capWidgetRef)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
return <div id={MOUNT_ID} className="m-captcha" data-ui-id="ui.captcha.mount" />;
|
||||
}
|
||||
|
||||
function createCapWidget(instanceUrl: string, sitekey: string) {
|
||||
function createCapWidget(instanceUrl: string, sitekey: string, ref: Ref<HTMLElement>) {
|
||||
return createElement("cap-widget", {
|
||||
ref,
|
||||
required: true,
|
||||
"data-cap-api-endpoint": `${instanceUrl.replace(/\/+$/, "")}/${sitekey}/`,
|
||||
"data-ui-id": "ui.captcha.mount",
|
||||
@@ -88,6 +113,8 @@ function loadCapAssets(instanceUrl: string): void {
|
||||
script.defer = true;
|
||||
script.src = scriptSrc;
|
||||
script.dataset.capWidgetLoader = "true";
|
||||
const nonce = getCurrentDocumentCspNonce(document);
|
||||
if (nonce) script.nonce = nonce;
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user