Alokai
Advanced

reCAPTCHA

Verify a Google reCAPTCHA token before the Magento integration signs in or registers a customer, resets a password, or adds a product review.

Google reCAPTCHA checks that a form was filled in by a person. When you turn it on in the Magento integration, the middleware asks Google to verify a reCAPTCHA token before it signs in or registers a customer, sends a password reset email, resets a password, or adds a product review. A missing or rejected token gets a 403 response.

Sign-in and registration need a token too

The unified loginCustomer and registerCustomer methods don't send a reCAPTCHA token, so with reCAPTCHA on they respond with 403. Before you turn it on, change your storefront's sign-in and registration to get a token in the browser and pass it to generateCustomerToken and createCustomer - for example through a custom API method.

Configuration

The generated middleware config reads the reCAPTCHA settings from environment variables and passes them to the recaptcha object of the integration config:

Environment variablerecaptcha fieldValue
MAGENTO_RECAPTCHA_ENABLEDisEnabledtrue turns verification on. Any other value, or no value, leaves it off.
MAGENTO_RECAPTCHA_SITE_KEYsitekeyThe site key from the reCAPTCHA admin console. The browser uses it to get a token.
MAGENTO_RECAPTCHA_SECRET_KEYsecretkeyThe secret key from the same console. The middleware sends it to Google with each token.
MAGENTO_RECAPTCHA_VERSIONversion2 or 3.
MAGENTO_RECAPTCHA_MIN_SCOREscoreRequired by the generated config when reCAPTCHA is on. The middleware doesn't check the score.
MAGENTO_RECAPTCHA_ENABLED=true
MAGENTO_RECAPTCHA_SITE_KEY=<your-site-key>
MAGENTO_RECAPTCHA_SECRET_KEY=<your-secret-key>
MAGENTO_RECAPTCHA_VERSION=3
MAGENTO_RECAPTCHA_MIN_SCORE=0.5

When MAGENTO_RECAPTCHA_ENABLED is true, the generated config throws at startup if any of the other four variables is missing:

const {
  // ...
  MAGENTO_RECAPTCHA_ENABLED,
  MAGENTO_RECAPTCHA_MIN_SCORE,
  MAGENTO_RECAPTCHA_SECRET_KEY,
  MAGENTO_RECAPTCHA_SITE_KEY,
  MAGENTO_RECAPTCHA_VERSION,
} = process.env;

const isRecaptchaEnabled = MAGENTO_RECAPTCHA_ENABLED === 'true';
if (isRecaptchaEnabled) {
  if (!MAGENTO_RECAPTCHA_SITE_KEY) throw new Error('Missing env var: MAGENTO_RECAPTCHA_SITE_KEY');
  if (!MAGENTO_RECAPTCHA_SECRET_KEY)
    throw new Error('Missing env var: MAGENTO_RECAPTCHA_SECRET_KEY');
  if (!MAGENTO_RECAPTCHA_VERSION) throw new Error('Missing env var: MAGENTO_RECAPTCHA_VERSION');
  if (!MAGENTO_RECAPTCHA_MIN_SCORE) throw new Error('Missing env var: MAGENTO_RECAPTCHA_MIN_SCORE');
}

export const config = {
  configuration: {
    // ...
    recaptcha: {
      isEnabled: isRecaptchaEnabled,
      score: Number.parseFloat(MAGENTO_RECAPTCHA_MIN_SCORE ?? ''),
      secretkey: MAGENTO_RECAPTCHA_SECRET_KEY || '',
      sitekey: MAGENTO_RECAPTCHA_SITE_KEY || '',
      version: Number.parseInt(MAGENTO_RECAPTCHA_VERSION ?? ''),
    },
  },
  // ...
} satisfies Integration<Config>;

Methods that need a token

With reCAPTCHA on, these API methods verify the recaptchaToken you pass in their first argument:

  • generateCustomerToken
  • createCustomer
  • requestPasswordResetEmail
  • resetPassword
  • createProductReview

Get the token in the browser with Google's reCAPTCHA script and your site key - see Google's guides for reCAPTCHA v2 and reCAPTCHA v3. Then pass it to the method:

import { sdk } from '~/sdk.config.ts';

// `recaptchaToken` holds the token that Google's reCAPTCHA script returned in the browser.
const result = await sdk.magento.generateCustomerToken({
  email: 'shopper@example.com',
  password: 'password',
  recaptchaToken,
});

When the token is missing or Google doesn't accept it, the method responds with 403 and the message Error during reCaptcha verification. Please try again.

To check that verification is on, call generateCustomerToken with an empty recaptchaToken. It should respond with that 403.

On this page