next-recaptcha-v3 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +205 -0
  3. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Roman Zhuravlov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,205 @@
1
+ <h1>⭐ Next.js ReCaptcha V3</h1>
2
+
3
+ Straightforward solution for using ReCaptcha in your [Next.js](https://nextjs.org/) application.
4
+
5
+ [![npm package](https://img.shields.io/npm/v/next-recaptcha-v3/latest.svg)](https://www.npmjs.com/package/next-recaptcha-v3)
6
+ ![Code style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)
7
+ ![type definition](https://img.shields.io/npm/types/next-recaptcha-v3)
8
+
9
+ 🗜️ Tiny and Tree-Shakable
10
+
11
+ 🥰 Written in TypeScript
12
+
13
+ 🐅 Highly customizable
14
+
15
+ 😎 Uses `next/script` component
16
+
17
+ ## Install
18
+
19
+ ```ssh
20
+ yarn add next-recaptcha-v3
21
+ ```
22
+
23
+ or
24
+
25
+ ```ssh
26
+ npm install next-recaptcha-v3 --save
27
+ ```
28
+
29
+ ## Generate reCAPTCHA Key
30
+
31
+ To use ReCaptcha, you need to generate a `reCAPTCHA_site_key` for your site's domain. You can get one [here](https://www.google.com/recaptcha/intro/v3.html).
32
+
33
+ You can either add generated key as a [Next.js env variable](https://nextjs.org/docs/basic-features/environment-variables)
34
+
35
+ ```ssh
36
+ NEXT_PUBLIC_RECAPTCHA_SITE_KEY="GTM-XXXXXXX"
37
+ ```
38
+
39
+ or pass it directly to the `ReCaptchaProvider` using `reCaptchaKey` attribute.
40
+
41
+ ## Getting Started
42
+
43
+ Wrap your application with `ReCaptchaProvider`.
44
+ It will load [ReCaptcha script](https://www.google.com/recaptcha/api.js) to your document.
45
+
46
+ ```tsx
47
+ import { ReCaptchaProvider } from "next-recaptcha-v3";
48
+
49
+ const MyApp = ({ Component, pageProps }) => (
50
+ <ReCaptchaProvider reCaptchaKey="[GTM-XXXXXXX]">
51
+ <Component {...pageProps} />
52
+ </ReCaptchaProvider>
53
+ );
54
+ ```
55
+
56
+ `ReCaptchaProvider` uses [Next.js Script](https://nextjs.org/docs/basic-features/script) to add ReCaptcha script to the document.
57
+
58
+ ## ReCaptchaProvider Props
59
+
60
+ | **Prop** | **Type** | **Default** | **Required** | **Description** |
61
+ | --------------- | -------- | ----------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
62
+ | reCaptchaKey | string | | No | Your reCAPTCHA key, get one from [here](https://www.google.com/recaptcha/about) |
63
+ | useEnterprise | boolean | false | No | Set to `true` if you use [ReCaptcha Enterprise](https://cloud.google.com/recaptcha-enterprise) |
64
+ | useRecaptchaNet | boolean | false | No | Set to `true` if you want to use `recaptcha.net` to load ReCaptcha script. [docs](https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally) |
65
+ | language | string | | No | Optional [Language Code](https://developers.google.com/recaptcha/docs/language) |
66
+
67
+ All extra props are passed directly to the Script tag, so you can use all props from the [next/script documentation](https://nextjs.org/docs/api-reference/next/script).
68
+
69
+ ### reCAPTCHA Enterprise
70
+
71
+ If you're using [reCAPTCHA Enterprise](https://cloud.google.com/recaptcha-enterprise), add `useEnterprise` to your `ReCaptchaProvider`. Checkout official quickstart guide [here](https://cloud.google.com/recaptcha-enterprise/docs/quickstart).
72
+
73
+ ```tsx
74
+ import { ReCaptchaProvider } from "next-recaptcha-v3";
75
+
76
+ const MyApp = ({ Component, pageProps }) => (
77
+ <ReCaptchaProvider useEnterprise>
78
+ <Component {...pageProps} />
79
+ </ReCaptchaProvider>
80
+ );
81
+ ```
82
+
83
+ ## Usage
84
+
85
+ When invoked, ReCaptcha will analyze the user's behavior and create a one-time [token](https://developers.google.com/recaptcha/docs/v3#programmatically_invoke_the_challenge). It can only be used once and is only valid for a couple of minutes, so you should generate it just before the actual validation.
86
+
87
+ Send the resulting token to the API request to your server. You can then decrypt the token using the ReCaptcha [/siteverify](https://developers.google.com/recaptcha/docs/verify) API and ignore the call if it came from a bot.
88
+
89
+ 1. React Hook: `useReCaptcha` (recommended approach)
90
+
91
+ Use `executeRecaptcha` function returned from the `useReCaptcha` hook to generate token. Add a unique action name to better understand at what moment the token was generated
92
+
93
+ ```tsx
94
+ import { useState } from "react";
95
+ import { useReCaptcha } from "next-recaptcha-v3";
96
+
97
+ const MyForm = () => {
98
+ const [name, setName] = useState("");
99
+
100
+ // Import 'executeRecaptcha' using 'useReCaptcha' hook
101
+ const { executeRecaptcha } = useReCaptcha();
102
+
103
+ const handleSubmit = useCallback(async (e) => {
104
+ e.preventDefault();
105
+
106
+ // Generate ReCaptcha token
107
+ const token = await executeRecaptcha("form-submit");
108
+
109
+ // Attach generated token to your API requests and validate it on the server
110
+ fetch("/api/form-submit", {
111
+ method: "POST",
112
+ body: {
113
+ data: { name },
114
+ token,
115
+ },
116
+ });
117
+ }, []);
118
+
119
+ return (
120
+ <form onSubmit={handleSubmit}>
121
+ <input name="name" value={name} onChange={(e) => setName(e.target.value)} />
122
+ <button type="submit">Submit</button>
123
+ </form>
124
+ );
125
+ };
126
+ ```
127
+
128
+ 2. `ReCaptcha` component
129
+
130
+ Alternatively, you can also generate token by using `ReCaptcha` component.
131
+
132
+ ```tsx
133
+ import { useEffect } from "react";
134
+ import { ReCaptcha } from "next-recaptcha-v3";
135
+ import { validateToken } from "./utils";
136
+
137
+ const MyPage = () => {
138
+ const [token, setToken] = useState<string>(null);
139
+
140
+ useEffect(() => {
141
+ if (token) {
142
+ // Validate token and make some actions if it's a bot
143
+ validateToken(token);
144
+ }
145
+ }, [token]);
146
+
147
+ return (
148
+ <>
149
+ <GoogleReCaptcha onVerify={setToken} action="page-view" />
150
+ <h1>Hello</h1>
151
+ </>
152
+ );
153
+ };
154
+ ```
155
+
156
+ 3. `withReCaptcha` HOC
157
+
158
+ ```tsx
159
+ import { useEffect } from "react";
160
+ import { withReCaptcha, WithReCaptchaProps } from "next-recaptcha-v3";
161
+ import { validateToken } from "./utils";
162
+
163
+ interface MyPageProps extends WithReCaptchaProps {}
164
+
165
+ const MyPage: React.FC<MyPageProps> = ({ executeRecaptcha }) => {
166
+ const [token, setToken] = useState<string>(null);
167
+
168
+ useEffect(() => {
169
+ if (typeof executeRecaptcha === "function") {
170
+ const generateToken = async () => {
171
+ const newToken = await executeRecaptcha("page-view");
172
+ setToken(newToken);
173
+ };
174
+ generateToken();
175
+ }
176
+ }, [executeRecaptcha]);
177
+
178
+ useEffect(() => {
179
+ if (token) {
180
+ // Validate token and make some actions if it's a bot
181
+ validateToken(token);
182
+ }
183
+ }, [token]);
184
+
185
+ return <h1>Hello</h1>;
186
+ };
187
+
188
+ export default withReCaptcha(MyPage);
189
+ ```
190
+
191
+ ## TypeScript
192
+
193
+ The module is written in TypeScript and type definitions are included.
194
+
195
+ ## Contributing
196
+
197
+ Contributions, issues and feature requests are welcome!
198
+
199
+ ## Show your support
200
+
201
+ Give a ⭐️ if you like this project!
202
+
203
+ ## LICENSE
204
+
205
+ [MIT](./LICENSE)
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "next-recaptcha-v3",
3
+ "version": "0.0.1",
4
+ "description": "🤖 Next.js hook to add Google ReCaptcha to your application",
5
+ "license": "MIT",
6
+ "author": "Roman Zhuravlov",
7
+ "repository": "https://github.com/snelsi/next-recaptcha-v3",
8
+ "homepage": "https://github.com/snelsi/next-recaptcha-v3",
9
+ "keywords": [
10
+ "recaptcha",
11
+ "recaptcha-v3",
12
+ "google-recaptcha-v3",
13
+ "next",
14
+ "next.js",
15
+ "react",
16
+ "hook"
17
+ ],
18
+ "main": "./lib/cjs/index.js",
19
+ "module": "./lib/esm/index.js",
20
+ "types": "./lib/esm/index.d.ts",
21
+ "files": [
22
+ "/lib"
23
+ ],
24
+ "scripts": {
25
+ "build": "yarn build:esm && yarn build:cjs",
26
+ "build:esm": "tsc",
27
+ "build:cjs": "tsc --module commonjs --outDir lib/cjs",
28
+ "prettier": "prettier --write .",
29
+ "lint": "eslint",
30
+ "lint-fix": "eslint --fix",
31
+ "fix": "npm run prettier && npm run lint-fix",
32
+ "pre-commit": "npm run lint && lint-staged",
33
+ "post-commit": "git update-index --again"
34
+ },
35
+ "peerDependencies": {
36
+ "next": ">=11.0.0",
37
+ "react": ">=16.8.0",
38
+ "react-dom": ">=16.8.0"
39
+ },
40
+ "engines": {
41
+ "node": ">=12.22.0"
42
+ },
43
+ "dependencies": {
44
+ "next": "^12.1.6",
45
+ "react": "^18.1.0",
46
+ "react-dom": "^18.1.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^17.0.34",
50
+ "@types/react": "^18.0.9",
51
+ "@types/react-dom": "^18.0.4",
52
+ "@typescript-eslint/eslint-plugin": "^5.25.0",
53
+ "@typescript-eslint/parser": "^5.25.0",
54
+ "eslint": "^8.15.0",
55
+ "eslint-config-next": "^12.1.6",
56
+ "eslint-config-prettier": "^8.5.0",
57
+ "husky": "^8.0.1",
58
+ "lint-staged": "^12.4.1",
59
+ "prettier": "^2.6.2",
60
+ "pretty-quick": "^3.1.3",
61
+ "typescript": "^4.6.4"
62
+ },
63
+ "lint-staged": {
64
+ "**/*.{js,jsx,ts,tsx}": [
65
+ "pretty-quick --staged"
66
+ ]
67
+ }
68
+ }