0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-captcha/assets/javascripts/pre-initializers/h-captcha.js
Juan David Martínez Cubillos 04b5530f34
DEV: Plugin rename discourse captcha (#39350)
####  Description:
This PR renames the `discourse-hcaptcha` plugin to `discourse-captcha`
to better reflect its support for multiple captcha providers (hCaptcha
and reCAPTCHA)

#### Changes

- Renamed plugin directory from plugins/discourse-hcaptcha to
plugins/discourse-captcha
- Updated Ruby module namespace from DiscourseHcaptcha to
DiscourseCaptcha
  - Updated references in:
    - .gitignore
    - pnpm-workspace.yaml
    - tsconfig.json
    - config/official_plugins.json
    - translator.yml
    - migrations/core/config/intermediate_db.yml
    - `hosted-site` plugin
- Migration file preserved to handle existing discourse_hcaptcha_enabled
setting migration

---------

Co-authored-by: Gabriel Grubba <gabriel@discourse.org>
2026-07-21 17:21:39 -05:00

64 lines
1.5 KiB
JavaScript
Vendored

import { ajax } from "discourse/lib/ajax";
import { withPluginApi } from "discourse/lib/plugin-api";
import { i18n } from "discourse-i18n";
function captchaSelector(siteSettings) {
if (siteSettings.discourse_captcha_provider !== "none") {
return siteSettings.discourse_captcha_provider;
} else {
return false;
}
}
function initializeHCaptcha(api, container) {
const siteSettings = container.lookup("service:site-settings");
if (
!siteSettings.discourse_captcha_enabled ||
!captchaSelector(siteSettings)
) {
return;
}
api.registerBehaviorTransformer("create-account", async ({ next }) => {
const captchaService = container.lookup("service:captcha-service");
captchaService.submitted = true;
if (captchaService.invalid) {
return {
success: false,
message: i18n("discourse_captcha.missing_token"),
};
}
const captchaRoute = captchaSelector(siteSettings);
try {
await ajax(`/captcha/${captchaRoute}/create.json`, {
data: { token: captchaService.token },
type: "POST",
});
} catch {
captchaService.reset();
return {
success: false,
message: i18n("discourse_captcha.verification_failed"),
};
}
try {
return await next();
} finally {
captchaService.reset();
}
});
}
export default {
name: "hcaptcha-initializer",
before: "inject-discourse-objects",
initialize(container) {
withPluginApi((api) => initializeHCaptcha(api, container));
},
};