2.0.1 • Published 6 months ago

payload-recaptcha-v3 v2.0.1

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

Payload reCAPTCHA v3 Plugin

NPM CI Downloads

A plugin for Payload to protect collection's operations using 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
# OR
npm i payload-recaptcha-v3

Configuration

In the plugins array of your Payload config, call the plugin with options:

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

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

export default config;

Plugin Options

  • secret: string

    Required. Your Google reCAPTCHA v3 secret key.

  • errorHandler: reCAPTCHAErrorHandler

    Optional. The function that throws the exception. By default, it throws Forbidden when the response from Google is not a success.

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 { CollectionConfig } from 'payload/types';

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>

Tests

Tests are using Jest, to run the tests use:

npm test

Types

reCAPTCHAErrorHandler

A function that has the purpose of throwing an exception depending on the response received from Google.

type reCAPTCHAErrorHandler = (response?: reCAPTCHAResponse) => void;

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-keysUnknown
1.2.0

6 months ago

1.1.0

7 months ago

1.0.0

7 months ago

2.0.1

6 months ago

1.2.1

6 months ago

2.0.0

6 months ago

1.0.0-alpha.4

8 months ago

1.0.0-alpha.3

8 months ago

1.0.0-alpha.2

8 months ago

1.0.0-alpha.1

8 months ago