diff --git a/README.md b/README.md index 5f818ee..88a6f14 100755 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ This package, `django-hoptcha`, allows Django developers to quickly integrate Ho * [Custom Rate-Limiting Logic](#custom-rate-limiting-logic) * [Combining with Django-Ratelimit](#combining-with-django-ratelimit) * [Custom CAPTCHA UI Rendering](#custom-captcha-ui-rendering) + * [Custom fetch function](#custom-fetch-function) * [Customization](#customization) * [License](#license) @@ -185,7 +186,17 @@ You can override the default iframe and style using the optional onCaptcha param ```javascript hoptchaPost('/endpoint', payload, onSuccess, onError, function renderCustom(url) { // Replace container with your custom implementation - $('#myCustomCaptchaArea').html(``); + document.getElementById('myCustomCaptchaArea').innerHTML = ``; +}); +``` + +### Custom fetch function + +You can use custom fetch function to handle requests. This can be useful in cases where you want to pass custom headers like CSRF protection headers. + +```javascript +configureHoptcha({ + fetcher: fetchWithAuth }); ``` diff --git a/django_hoptcha/static/django_hoptcha/hoptcha.js b/django_hoptcha/static/django_hoptcha/hoptcha.js index a0f50ee..3800a26 100755 --- a/django_hoptcha/static/django_hoptcha/hoptcha.js +++ b/django_hoptcha/static/django_hoptcha/hoptcha.js @@ -31,6 +31,10 @@ } }; + window.configureHoptcha = function ({ fetcher }) { + if (fetcher) window.hoptchaFetcher = fetcher; + }; + /** * Utility for retrying failed requests that require CAPTCHA. * @param {string} url - The POST endpoint. @@ -39,8 +43,12 @@ * @param {function} [onError] - Error fallback. * @param {function} [onCaptcha] - Optional custom render callback. */ - window.hoptchaPost = function (url, payload, onSuccess, onError, onCaptcha) { - fetch(url, { + window.hoptchaPost = function (url, + payload, + onSuccess, onError, onCaptcha) { + const fetcher = window.hoptchaFetcher || fetch; + + fetcher(url, { method: "POST", headers: { "Content-Type": "application/json", @@ -71,7 +79,7 @@ // Register callback window._captchaSuccessCallback = function (token) { payload.captcha_token = token; - window.hoptchaPost(url, payload, onSuccess, onError, onCaptcha); + window.hoptchaPost(url, payload, onSuccess, onError, onCaptcha, fetcher); }; const render = onCaptcha || window.renderCaptchaStep;