@redwoodjs/sdk
Version:
A full-stack webapp toolkit designed for TypeScript, Vite, and React Server Components
24 lines (23 loc) • 811 B
JavaScript
"use client";
import { useRef, useCallback } from "react";
import { IS_DEV } from "../../constants";
export function useTurnstile(siteKey) {
const containerRef = useRef(null);
const resolverRef = useRef(Promise.withResolvers());
const widgetIdRef = useRef(null);
const challenge = useCallback(async () => {
if (!widgetIdRef.current &&
containerRef.current &&
window.turnstile) {
widgetIdRef.current = window.turnstile.render(containerRef.current, {
sitekey: IS_DEV ? "1x00000000000000000000AA" : siteKey,
callback: (token) => resolverRef.current.resolve(token),
});
}
return resolverRef.current.promise;
}, [siteKey]);
return {
ref: containerRef,
challenge,
};
}