GeorgeHulpoi

payload recaptcha v3

by GeorgeHulpoi
Forms
v3
^3.0.0

Overview

Summary generated from the README

Verifies Google reCAPTCHA v3 tokens before selected collection operations run. You register the plugin with your secret key, then list protected operations and actions in each collection's custom config. Clients send the token in the x-recaptcha-v3 header. Requires Payload 3.

Features

  • Protects chosen collection operations such as create, update and delete
  • Reads the token from the x-recaptcha-v3 request header
  • Configurable score threshold, default 0.7, overridable per operation
  • Optional skip callback to bypass checks, for example for admins
  • Custom error handler, with a Forbidden error thrown by default
  • Checks that the returned action matches the configured action

Use cases

  • Block bot submissions to a collection that accepts public create requests
  • Protect order or contact endpoints while letting admin users skip verification

From the author

This library implements a collection protection in Payload CMS using Google reCAPTCHA v3.

Installation

npm i payload-recaptcha-v3

37

Stars

4

Forks

2

Open Issues

1 years ago

Last Updated

183

Weekly Downloads

v3.1.1

npm Version

353.5 KB

Unpacked Size

1

Dependencies

GitHub stars

37+2 since Sep 7

Show data
GitHub stars by day (UTC)
DateStars
Dec 6, 202637
Dec 5, 202637
Dec 4, 202637
Dec 3, 202637
Dec 2, 202637
Dec 1, 202637
Nov 30, 202637
Nov 29, 202637
Nov 28, 202637
Nov 27, 202637
Nov 26, 202637
Nov 25, 202637
Nov 24, 202637
Nov 23, 202637
Nov 22, 202637
Nov 21, 202637
Nov 20, 202637
Nov 19, 202637
Nov 18, 202637
Nov 17, 202637
Nov 16, 202637
Nov 15, 202637
Nov 14, 202637
Nov 13, 202637
Nov 12, 202637
Nov 11, 202637
Nov 10, 202636
Nov 9, 202636
Nov 8, 202636
Nov 7, 202636
Nov 6, 202636
Nov 5, 202636
Nov 4, 202636
Nov 3, 202635
Nov 2, 202635
Nov 1, 202635
Oct 31, 202635
Oct 30, 202635
Oct 29, 202635
Oct 28, 202635
Oct 27, 202635
Oct 26, 202635
Oct 25, 202635
Oct 24, 202635
Oct 23, 202635
Oct 22, 202635
Oct 21, 202635
Oct 20, 202635
Oct 19, 202635
Oct 18, 202635
Oct 17, 202635
Oct 16, 202635
Oct 15, 202635
Oct 14, 202635
Oct 13, 202635
Oct 12, 202635
Oct 11, 202635
Oct 10, 202635
Oct 9, 202635
Oct 8, 202635
Oct 7, 202635
Oct 6, 202635
Oct 5, 202635
Oct 4, 202635
Oct 3, 202635
Oct 2, 202635
Oct 1, 202635
Sep 30, 202635
Sep 29, 202635
Sep 28, 202635
Sep 27, 202635
Sep 26, 202635
Sep 25, 202635
Sep 24, 202635
Sep 23, 202635
Sep 22, 202635
Sep 21, 202635
Sep 20, 202635
Sep 19, 202635
Sep 18, 202635
Sep 17, 202635
Sep 16, 202635
Sep 15, 202635
Sep 14, 202635
Sep 13, 202635
Sep 12, 202635
Sep 11, 202635
Sep 10, 202635
Sep 9, 202635
Sep 8, 202635
Sep 7, 202635
npm weekly downloads

200+121 since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 2026200
Dec 5, 2026233
Dec 4, 2026213
Dec 3, 2026230
Dec 2, 2026243
Dec 1, 2026250
Nov 30, 2026252
Nov 29, 2026263
Nov 28, 2026250
Nov 27, 2026254
Nov 26, 2026276
Nov 25, 2026270
Nov 24, 2026271
Nov 23, 2026270
Nov 22, 2026282
Nov 21, 2026312
Nov 20, 2026340
Nov 19, 2026324
Nov 18, 2026335
Nov 17, 2026344
Nov 16, 2026333
Nov 15, 2026301
Nov 14, 2026253
Nov 13, 2026202
Nov 12, 2026188
Nov 11, 2026166
Nov 10, 2026146
Nov 9, 2026143
Nov 8, 2026153
Nov 7, 2026157
Nov 6, 2026160
Nov 5, 2026161
Nov 4, 2026172
Nov 3, 2026180
Nov 2, 2026182
Nov 1, 2026160
Oct 31, 2026152
Oct 30, 2026167
Oct 29, 2026133
Oct 28, 202694
Oct 27, 202676
Oct 26, 202693
Oct 25, 2026100
Oct 24, 2026140
Oct 23, 2026103
Oct 22, 2026135
Oct 21, 2026190
Oct 20, 2026188
Oct 19, 2026183
Oct 18, 2026191
Oct 17, 2026171
Oct 16, 2026220
Oct 15, 2026220
Oct 14, 2026241
Oct 13, 2026245
Oct 12, 2026228
Oct 11, 2026211
Oct 10, 2026211
Oct 9, 2026173
Oct 8, 2026168
Oct 7, 202697
Oct 6, 202695
Oct 5, 202690
Oct 4, 202692
Oct 3, 202695
Oct 2, 202696
Oct 1, 202698
Sep 30, 2026108
Sep 29, 2026136
Sep 28, 2026139
Sep 27, 2026155
Sep 26, 2026176
Sep 25, 2026192
Sep 24, 2026198
Sep 23, 2026206
Sep 22, 2026190
Sep 21, 2026188
Sep 20, 2026188
Sep 19, 2026160
Sep 18, 2026151
Sep 17, 2026135
Sep 16, 2026124
Sep 15, 2026109
Sep 14, 2026113
Sep 13, 2026108
Sep 12, 202691
Sep 11, 202686
Sep 10, 202687
Sep 9, 202684
Sep 8, 202683
Sep 7, 202679

Each point is npm's rolling 7-day download count.

Health Score

37/100Fair

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/679237214-root.svg)](https://payloaddirectory.dev/plugins/679237214-root)
HTML
<a href="https://payloaddirectory.dev/plugins/679237214-root"><img src="https://payloaddirectory.dev/badge/679237214-root.svg" alt="Payload Directory"></a>
Updated Mar 6, 2025Created Aug 16, 2023Published Mar 6, 2025

README

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

  • secret: string Required. Your Google reCAPTCHA v3 secret key.
  • errorHandler: reCAPTCHAErrorHandler Optional. See more details
  • skip: reCAPTCHASkip Optional. See more details
  • scoreThreshold: number Optional. A value between 0 and 1 (default is 0.7). The received score from Google reCAPTCHA must be greater or equal than threshold.

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.

Similar plugins