Change captcha logic
This commit is contained in:
parent
f57fa5b293
commit
17e7ae140e
@ -121,9 +121,9 @@ def hoptcha_protected(
|
|||||||
|
|
||||||
if not token or not verify_token(token):
|
if not token or not verify_token(token):
|
||||||
return response(request) if response else JsonResponse({
|
return response(request) if response else JsonResponse({
|
||||||
"error": "CAPTCHA",
|
"captcha": True,
|
||||||
"url": f"{GENERATE_URL}?{urlencode({'client_key': PUBLIC_KEY, 'timestamp': int(time.time() * 1000), 'type': type})}"
|
"url": f"{GENERATE_URL}?{urlencode({'client_key': PUBLIC_KEY, 'timestamp': int(time.time() * 1000), 'type': type})}"
|
||||||
}, status=400)
|
}, status=200)
|
||||||
else:
|
else:
|
||||||
cache.delete(cache_key) # reset counter if passed
|
cache.delete(cache_key) # reset counter if passed
|
||||||
return view_func(request, *args, **kwargs)
|
return view_func(request, *args, **kwargs)
|
||||||
|
@ -56,34 +56,38 @@
|
|||||||
body: JSON.stringify(payload)
|
body: JSON.stringify(payload)
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response.ok) {
|
return response.json().then(data => {
|
||||||
return response.json().then(data => {
|
return { ok: response.ok, data };
|
||||||
throw { status: response.status, data };
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(({ ok, data }) => {
|
||||||
if (onSuccess) onSuccess(data);
|
const captchaNeeded = data?.captcha === true;
|
||||||
})
|
const captchaUrl = data?.url;
|
||||||
.catch(err => {
|
|
||||||
const error = err.data?.error || 'Something went wrong.';
|
|
||||||
const captcha_url = err.data?.url;
|
|
||||||
|
|
||||||
if (!captcha_url) {
|
if (captchaNeeded && captchaUrl) {
|
||||||
if (onError) onError(error);
|
// Register retry callback
|
||||||
else console.error(error);
|
window._captchaSuccessCallback = function (token) {
|
||||||
|
payload.captcha_token = token;
|
||||||
|
window.hoptchaPost(url, payload, onSuccess, onError, onCaptcha);
|
||||||
|
};
|
||||||
|
|
||||||
|
const render = onCaptcha || window.renderCaptchaStep;
|
||||||
|
render(captchaUrl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register callback
|
if (ok) {
|
||||||
window._captchaSuccessCallback = function (token) {
|
if (onSuccess) onSuccess(data);
|
||||||
payload.captcha_token = token;
|
} else {
|
||||||
window.hoptchaPost(url, payload, onSuccess, onError, onCaptcha);
|
const error = data?.error || 'Something went wrong.';
|
||||||
};
|
if (onError) onError(error);
|
||||||
|
else console.error(error);
|
||||||
const render = onCaptcha || window.renderCaptchaStep;
|
}
|
||||||
render(captcha_url);
|
})
|
||||||
|
.catch(err => {
|
||||||
|
const fallback = typeof err === 'string' ? err : err?.data?.error || 'Something went wrong.';
|
||||||
|
if (onError) onError(fallback);
|
||||||
|
else console.error(fallback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
})(window);
|
})(window);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user