@graphcommerce/googlerecaptcha 2.0.5 → 2.1.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#1344](https://github.com/graphcommerce-org/graphcommerce/pull/1344) [`5ab19683c`](https://github.com/graphcommerce-org/graphcommerce/commit/5ab19683ca4e1ff78c21dbe09b73aeea43ef3b64) Thanks [@paales](https://github.com/paales)! - Added Google reCAPTCHA conditional loading to make it fully compatible, but load the reCAPTCHA only on pages with forms.
8
+
3
9
  ## 2.0.5
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -1,14 +1,28 @@
1
1
  # @graphcommerce/googlerecaptcha
2
2
 
3
- This package makes it easy to add Google Recaptcha v3 to GraphCommerce
3
+ This package makes it easy to add Google Recaptcha v3 to GraphCommerce. It
4
+ allows you to load the Recatcha script conditionally on the page so it isn't
5
+ initialized on all pages.
4
6
 
5
- ### Usage
7
+ ### Installation
6
8
 
7
9
  1. Add `NEXT_PUBLIC_GOOGLE_RECAPTCHA_V3_SITE_KEY` to your .env file.
8
10
  [example](../../examples/magento-graphcms/.env.example)
9
- 2. Add `<GoogleRecaptchaV3Script/>` to your `pages/_app.tsx` file
11
+ 2. Add `<GoogleRecaptchaProvider/>` to your `pages/_app.tsx` file
10
12
  [example](../../examples/magento-graphcms/pages/_app.tsx)
11
13
  3. Add `recaptchaLink` to your Apollo Client.
12
- [example](../../examples/magento-graphcms/lib/createApolloClient.ts)
14
+ [example](../../examples/magento-graphcms/lib/graphql/GraphQLProvider.tsx)
13
15
  4. Add `X-Recaptcha` header to your `.meshrc.yml`.
14
16
  [example](../../examples/magento-graphcms/.meshrc.yml)
17
+
18
+ ### Usage
19
+
20
+ If you have a form that uses GoogleRecaptcha add `useGoogleRecaptcha()` in the
21
+ root of your component.
22
+
23
+ ### Troubleshooting
24
+
25
+ `ReCaptcha validation failed, please try again`
26
+ : Magento is configured correctly and this is an error coming from Google
27
+ directly. You have misconfigured something on the Google side. Make sure the
28
+ domains are set up correctly, etc.
@@ -0,0 +1,20 @@
1
+ import React, { useMemo, useState } from 'react'
2
+ import { RecaptchaContext, recaptchaContext } from '../context/recaptchaContext'
3
+ import { GoogleRecaptchaV3Script } from './GoogleRecaptchaV3Script'
4
+
5
+ export function GoogleRecaptchaProvider(props: { children: React.ReactNode; siteKey?: string }) {
6
+ const { children, siteKey = process.env.NEXT_PUBLIC_GOOGLE_RECAPTCHA_V3_SITE_KEY } = props
7
+ const [enabled, setEnabled] = useState(false)
8
+
9
+ const context: RecaptchaContext = useMemo(
10
+ () => ({ enabled, siteKey, enable: () => setEnabled(true) }),
11
+ [enabled, siteKey],
12
+ )
13
+
14
+ return (
15
+ <recaptchaContext.Provider value={context}>
16
+ <GoogleRecaptchaV3Script />
17
+ {children}
18
+ </recaptchaContext.Provider>
19
+ )
20
+ }
@@ -1,23 +1,26 @@
1
+ import { GlobalStyles } from '@mui/material'
1
2
  import Script from 'next/script'
2
- import React from 'react'
3
+ import { useContext } from 'react'
4
+ import { recaptchaContext } from '../context/recaptchaContext'
3
5
 
4
6
  export function GoogleRecaptchaV3Script() {
5
- const siteKey = process.env.NEXT_PUBLIC_GOOGLE_RECAPTCHA_V3_SITE_KEY
7
+ const { siteKey, enabled } = useContext(recaptchaContext)
6
8
 
7
- if (process.env.NODE_ENV !== 'production' && !siteKey)
9
+ if (process.env.NODE_ENV !== 'production' && !siteKey && enabled)
8
10
  console.warn(
9
11
  '[@graphcommerce/googletagmanager]: NEXT_PUBLIC_GOOGLE_RECAPTCHA_V3_SITE_KEY not found',
10
12
  )
11
13
 
12
- if (!siteKey) return null
14
+ if (!siteKey || !enabled) return null
13
15
 
14
16
  return (
15
17
  <>
16
18
  <Script
19
+ key='google-recaptcha-v3'
17
20
  strategy='lazyOnload'
18
21
  src={`https://www.google.com/recaptcha/api.js?render=${siteKey}`}
19
22
  />
20
- <style>{`.grecaptcha-badge': { visibility: 'hidden' }`}</style>
23
+ <GlobalStyles styles={{ '.grecaptcha-badge': { visibility: 'hidden' } }} />
21
24
  </>
22
25
  )
23
26
  }
@@ -0,0 +1,9 @@
1
+ import { createContext } from 'react'
2
+
3
+ export type RecaptchaContext = {
4
+ enabled: boolean
5
+ enable?: () => void
6
+ siteKey?: string
7
+ }
8
+
9
+ export const recaptchaContext = createContext<RecaptchaContext>({ enabled: false })
@@ -0,0 +1,4 @@
1
+ import { useContext } from 'react'
2
+ import { recaptchaContext } from '../context/recaptchaContext'
3
+
4
+ export const useGoogleRecaptcha = () => useContext(recaptchaContext).enable?.()
package/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './components/GoogleRecaptchaV3Script'
2
+ export * from './components/GoogleRecaptchaProvider'
2
3
  export * from './link/recaptchaLink'
4
+ export * from './hooks/useGoogleRecaptcha'
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/googlerecaptcha",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "2.0.5",
5
+ "version": "2.1.0",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {