3.1.1 • Published 3 months ago

payload-recaptcha-v3 v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Payload reCAPTCHA v3 Plugin

NPM CI Downloads

Payload reCAPTCHA v3 is a plugin for Payload used to protect collection operations with Google reCAPTCHA v3.

Installation

Please install the plugin version according to the Payload version. The major version of the plugin must match the major version of the Payload.

yarn add payload-recaptcha-v3
npm i payload-recaptcha-v3
pnpm add payload-recaptcha-v3

Configuration

In the plugins property of Payload config, call the plugin with the following options:

import { buildConfig } from 'payload';
import reCAPTCHAv3 from 'payload-recaptcha-v3';

export default buildConfig({
	// ... rest of your config
	plugins: [
		reCAPTCHAv3({
			secret: process.env.GOOGLE_RECAPTCHA_SECRET!,
		}),
	],
});

Plugin Options

Usage

To protect a collection's operation, you have to add in the Collection Config the property recaptcha into the custom. The recaptcha property has to be an array of strings containing the operation name according to Available Collection operations.

import type { CollectionConfig } from 'payload';

export const Orders: CollectionConfig = {
	slug: 'orders',
	fields: [],
	// ... rest of your config
	custom: {
		recaptcha: [
			{
				name: 'create',
				action: 'submit',
			},
			{
				name: 'update',
				action: 'modify',
			},
		],
	},
};

export default Orders;

Then, when you make an HTTP Request to the Payload API, include the header x-recaptcha-v3 with the token received from Google:

   <script>
      function onClick(e) {
        e.preventDefault();
        grecaptcha.ready(function() {
          grecaptcha.execute('reCAPTCHA_site_key', {action: 'submit'}).then(function(token) {
            fetch('/api/orders', {
              method: 'POST',
              headers: {
                'x-recaptcha-v3': token
              },
              body: JSON.stringify({...})
            })
          });
        });
      }
  </script>

Optionally, you can set a errorHandler or skip as described in Plugin Options in a specific operation.

import type { CollectionConfig } from 'payload';

export const Orders: CollectionConfig = {
	slug: 'orders',
	fields: [],
	// ... rest of your config
	custom: {
		recaptcha: [
			{
				name: 'create',
				action: 'submit',
				errorHandler: (args) => {
					// ...
				},
			},
			{
				name: 'update',
				action: 'modify',
				skip: (args) => {
					// ....
				},
			},
			{
				name: 'delete',
				action: 'delete',
				scoreThreshold: 0.9,
			}
		],
	},
};

export default Orders;

Tests

This plugin uses Playwright for end-to-end testing with Google reCAPTCHA directly. However, there are some steps to test the plugin.

  1. Provide DATABASE_URI, RECAPTCHA_SECRET and NEXT_PUBLIC_RECAPTCHA_SITE_KEY environment variables.
  2. Run pnpm build and then pnpm dev:build.
  3. Run pnpm test.

Types

reCAPTCHASkip

A callback function that when returns true, it will skip the Google reCAPTCHA verification (for example, when the user is an admin).

The arguments of function are the same of Before Operation Hook.

export type reCAPTCHASkip = (
	args: Parameters<CollectionBeforeOperationHook>[0],
) => boolean;

reCAPTCHAErrorHandler

A callback function that is called when:

  • The header x-recaptcha-v3 is not set.
  • The fetch request generated an error.
  • The response from Google was not a success.
  • The response from Google was a success, but for another action.

When the errorHandler options property is not set, it will throw a Forbidden error by default.

export type reCAPTCHAErrorHandler = (args: {
	hookArgs: Parameters<CollectionBeforeOperationHook>[0];
	response?: reCAPTCHAResponse;
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	error?: any;
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) => any;

reCAPTCHAResponse

The response received from Google when verifying the token.

Properties

NameDescription
success: booleanwhether this request was a valid reCAPTCHA token for your site
score: numberthe score for this request (0.0 - 1.0)
action: stringthe action name for this request (important to verify)
challenge_ts: numbertimestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
hostname: stringthe hostname of the site where the reCAPTCHA was solved
'error-codes'?: reCAPTCHAErrorCode[]optional

reCAPTCHAErrorCode

Error codeDescription
missing-input-secretThe secret parameter is missing.
invalid-input-secretThe secret parameter is invalid or malformed.
missing-input-responseThe response parameter is missing.
invalid-input-responseThe response parameter is invalid or malformed.
bad-requestThe request is invalid or malformed.
timeout-or-duplicateThe response is no longer valid: either is too old or has been used previously.
invalid-keysThe secret key is incorrect.
3.1.1

3 months ago

3.1.0

7 months ago

3.0.0

7 months ago

3.0.0-beta.1

10 months ago

3.0.0-beta.0

11 months ago

3.0.0-beta.2

10 months ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

2.0.1

2 years ago

1.2.1

2 years ago

2.0.0

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-alpha.3

2 years ago

1.0.0-alpha.2

2 years ago

1.0.0-alpha.1

2 years ago