@adobe-commerce/recaptcha

中文文档
1.3.1发布于 2026/11/03SEE LICENSE IN LICENSE.mdES Module近 30 天下载 4606

Module allows to efficiently verify that users are humans, not bots or spammers

原始使用文档
文档目录12 节
  1. Purpose
  2. Installation
  3. Quick Start
  4. Public Methods
  5. setEndpoint(url: string)
  6. setConfig(configList: PropsFormTypes[], options?: { b2bEnab…
  7. initReCaptcha(lazyLoadTimeout?: number)
  8. verifyReCaptcha(): Promise<string | undefined>
  9. enableLogger(enabled: boolean)
  10. Default Form Types
  11. B2B Form Types
  12. Mixed Type Configuration

ReCaptcha Module

Purpose

This module integrates Google reCAPTCHA into Adobe Commerce storefronts. It supports both reCAPTCHA v3 and reCAPTCHA Enterprise, providing methods for configuring, initializing, and verifying reCAPTCHA on protected forms.

The module queries the recaptchaFormConfig GraphQL endpoint to fetch per-form configuration, automatically detecting which reCAPTCHA type (v3 or Enterprise) is configured and loading the appropriate script.

Installation

npm i @adobe-commerce/recaptcha

Quick Start

import {
  setEndpoint,
  setConfig,
  initReCaptcha,
  verifyReCaptcha,
  enableLogger,
} from '@adobe-commerce/recaptcha';

// 1. Set the backend GraphQL endpoint
setEndpoint('https://your-store.com/graphql');

// 2. Fetch and store reCAPTCHA configuration
await setConfig([{ badgeId: 'generateCustomerToken' }]);

// 3. Initialize reCAPTCHA (loads script, renders badges)
initReCaptcha();

// 4. Get a token when submitting a protected form
const token = await verifyReCaptcha();

Public Methods

setEndpoint(url: string)

Sets the GraphQL endpoint URL used to fetch reCAPTCHA configuration.

setConfig(configList: PropsFormTypes[], options?: { b2bEnabled?: boolean })

Fetches reCAPTCHA configuration from the backend via recaptchaFormConfig and stores it in session storage.

  • Queries all default form types and determines which are enabled
  • When options.b2bEnabled is true, includes COMPANY_CREATE in the query — this form type is not included by default
  • Normalizes the response into a unified config shape
  • Detects the reCAPTCHA type (v3 or Enterprise) from the re_captcha_type field
  • Skips forms with empty website_key
  • Warns and disables reCAPTCHA if mixed types (both v3 and Enterprise) are detected across forms
// B2C storefront (default)
await setConfig([{ badgeId: 'generateCustomerToken' }]);

// B2B storefront
await setConfig([], { b2bEnabled: true });

initReCaptcha(lazyLoadTimeout?: number)

Initializes reCAPTCHA by loading the appropriate Google script and rendering badges.

  • Loads enterprise.js for Enterprise or api.js for v3
  • For inline badge position, renders invisible widgets into DOM elements matching each form's badgeId
  • Default lazy load timeout: 3000ms

verifyReCaptcha(): Promise<string | undefined>

Generates and returns a reCAPTCHA token for form submission.

  • Uses grecaptcha.enterprise.execute() for Enterprise or grecaptcha.execute() for v3
  • Returns undefined if reCAPTCHA is not enabled or not configured

enableLogger(enabled: boolean)

Enables or disables debug logging. When enabled, logs are prefixed with [ReCaptcha] and include:

  • Config fetch and normalization details
  • Which reCAPTCHA type is detected and which forms are enabled
  • Script loading URLs
  • Badge rendering activity
  • Token generation attempts
  • Warnings for skipped forms (missing keys) or mixed type configurations

Default Form Types

These form types are queried by default (no additional configuration needed):

Form TypeBadge ID
PLACE_ORDERplaceOrder
CONTACTcontactUs
CUSTOMER_LOGINgenerateCustomerToken
CUSTOMER_FORGOT_PASSWORDrequestPasswordResetEmail
CUSTOMER_CREATEcreateCustomerV2
CUSTOMER_EDITupdateCustomerV2
NEWSLETTERsubscribeEmailToNewsletter
PRODUCT_REVIEWcreateProductReview
SENDFRIENDSENDFRIEND
BRAINTREEBRAINTREE

B2B Form Types

B2B storefronts should pass { b2bEnabled: true } to include COMPANY_CREATE in the reCAPTCHA query:

await setConfig([], { b2bEnabled: true });

This requires the backend to expose COMPANY_CREATE in ReCaptchaFormEnum.

Mixed Type Configuration

If the Commerce Admin has some forms configured with reCAPTCHA v3 and others with Enterprise, the module will:

  1. Log a warning with instructions to update the Commerce Admin configuration
  2. Disable reCAPTCHA entirely (returns no config, no script loaded)

All forms must use the same reCAPTCHA type. Configure this in the Commerce Admin under Stores > Configuration > Security > Google reCAPTCHA Storefront.

版本与发布记录

@adobe-commerce/recaptcha 共发布 140 个版本,首次发布于 2025/03/03;过去 12 个月发布了 126 个版本。

版本发布日期距今
1.3.1latest2026/11/032 天前
1.3.1-alpha-20261029084654alpha2026/10/297 天前
1.3.1-alpha-202610272012362026/10/289 天前
1.3.1-beta.0beta2026/10/2214 天前
1.3.1-alpha-202610212022512026/10/2215 天前
1.3.1-alpha-202610201602252026/10/2116 天前

关于 @adobe-commerce/recaptcha 的常见问题

@adobe-commerce/recaptcha 是什么?

Module allows to efficiently verify that users are humans, not bots or spammers

如何安装 @adobe-commerce/recaptcha?

在项目目录运行 npm install @adobe-commerce/recaptcha,或使用 pnpm add / yarn add / bun add。

@adobe-commerce/recaptcha 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/adobe-commerce__recaptcha 是否存在。

@adobe-commerce/recaptcha 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adobe-commerce/recaptcha 可以商用吗?

@adobe-commerce/recaptcha 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@adobe-commerce/recaptcha 的下载量有多少?

根据 npm 官方下载统计,@adobe-commerce/recaptcha 在 2026/10/05 – 2026/11/03 这 30 天内被下载 4,606 次,日均约 154 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adobe-commerce/recaptcha 还在维护吗?

最新版本 1.3.1 发布于 2026/11/03(2 天前);至今共发布 140 个版本,其中过去 12 个月发布了 126 个;首次发布于 2025/03/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。