Skip to content

Commit

Permalink
fix(api-form-builder): check both global and form recaptcha settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Jun 10, 2024
1 parent 1359b71 commit c1849b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { createTopic } from "@webiny/pubsub";
import { sanitizeFormSubmissionData } from "~/plugins/crud/utils/sanitizeFormSubmissionData";
import { mdbid } from "@webiny/utils";
import { FormsPermissions } from "~/plugins/crud/permissions/FormsPermissions";
import { isRecaptchaEnabled } from "~/plugins/crud/utils/isRecaptchaEnabled";

interface CreateSubmissionsCrudParams {
context: FormBuilderContext;
Expand Down Expand Up @@ -185,7 +186,7 @@ export const createSubmissionsCrud = (params: CreateSubmissionsCrudParams): Subm
throwOnNotFound: true
});

if (settings && settings.reCaptcha && settings.reCaptcha.enabled) {
if (settings && isRecaptchaEnabled(settings, form)) {
if (!reCaptchaResponseToken) {
throw new Error("Missing reCAPTCHA response token - cannot verify.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FbForm, Settings } from "~/types";

export const isRecaptchaEnabled = (settings: Settings, form: FbForm) => {
if (!settings.reCaptcha.enabled) {
return false;
}

return form.settings.reCaptcha?.enabled === true;
};
9 changes: 8 additions & 1 deletion packages/api-form-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ export interface FbFormField {
settings?: Record<string, any>;
}

export interface ReCaptchaSettings {
reCaptcha: {
enabled: boolean | null;
errorMessage: string;
};
}

export interface FbForm {
id: string;
tenant: string;
Expand All @@ -112,7 +119,7 @@ export interface FbForm {
fields: FbFormField[];
steps: FbFormStep[];
stats: Omit<FbFormStats, "conversionRate">;
settings: Record<string, any>;
settings: ReCaptchaSettings | Record<string, any>;
triggers: Record<string, any> | null;
formId: string;
webinyVersion: string;
Expand Down

0 comments on commit c1849b3

Please sign in to comment.