Skip to content

Commit

Permalink
Fix trying to use never-loaded recaptcha when submitting posts
Browse files Browse the repository at this point in the history
Closes #49
  • Loading branch information
dsevillamartin committed Feb 24, 2024
1 parent 47357eb commit e389ff0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions js/src/forum/extendComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { extend, override } from 'flarum/common/extend';
import RecaptchaState from '../common/states/RecaptchaState';
import Recaptcha from '../common/components/Recaptcha';

export const isRecaptchaConfigured = () => app.forum.attribute('fof-recaptcha.configured');
export const canPostWithoutCaptcha = () => app.forum.attribute('postWithoutCaptcha');
export const shouldUseCaptcha = () => isRecaptchaConfigured() && !canPostWithoutCaptcha();

export default function (Composer) {
extend(Composer.prototype, 'oninit', function () {
if (!app.forum.attribute('fof-recaptcha.configured') || app.forum.attribute('postWithoutCaptcha')) {
if (!shouldUseCaptcha()) {
return;
}

Expand All @@ -19,15 +23,15 @@ export default function (Composer) {
});

extend(Composer.prototype, 'data', function (data) {
if (!app.forum.attribute('fof-recaptcha.configured') || app.forum.attribute('postWithoutCaptcha')) {
if (!shouldUseCaptcha()) {
return;
}

data['g-recaptcha-response'] = this.recaptcha.getResponse();
});

extend(Composer.prototype, 'headerItems', function (fields) {
if (!app.forum.attribute('fof-recaptcha.configured') || app.forum.attribute('postWithoutCaptcha')) {
if (!shouldUseCaptcha()) {
return;
}

Expand All @@ -42,18 +46,15 @@ export default function (Composer) {

// There's no onerror handler on composer classes, but we can react to loaded which is called after errors
extend(Composer.prototype, 'loaded', function () {
if (!app.forum.attribute('fof-recaptcha.configured') || app.forum.attribute('postWithoutCaptcha')) {
if (!shouldUseCaptcha()) {
return;
}

this.recaptcha.reset();
});

override(Composer.prototype, 'onsubmit', function (original, argument1) {
if (
app.forum.attribute('fof-recaptcha.configured') ||
(!app.forum.attribute('postWithoutCaptcha') && this.recaptcha.isInvisible() && argument1 !== 'recaptchaSecondStep')
) {
if (shouldUseCaptcha() && this.recaptcha.isInvisible() && argument1 !== 'recaptchaSecondStep') {
this.loading = true;
this.recaptcha.execute();
return;
Expand Down

0 comments on commit e389ff0

Please sign in to comment.