@graphcommerce/googlerecaptcha 9.1.0-canary.16 → 9.1.0-canary.18

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,13 @@
1
1
  # Change Log
2
2
 
3
+ ## 9.1.0-canary.18
4
+
5
+ ## 9.1.0-canary.17
6
+
7
+ ### Patch Changes
8
+
9
+ - [#2496](https://github.com/graphcommerce-org/graphcommerce/pull/2496) [`a261149`](https://github.com/graphcommerce-org/graphcommerce/commit/a261149b6b32ad6a35606da2d530b8e41bfa10a0) - ReCaptcha now using the `recaptchaV3Config` query and add a fallback for Magento 2.4.6 and earlier to still use the configuration. Magento 2.4.7 doesn't need that configuration anymore. ([@paales](https://github.com/paales))
10
+
3
11
  ## 9.1.0-canary.16
4
12
 
5
13
  ## 9.1.0-canary.15
package/README.md CHANGED
@@ -7,8 +7,8 @@ page so it isn't initialized on all pages.
7
7
 
8
8
  ## Configuration
9
9
 
10
- 1. Configure the following ([configuration values](./Config.graphqls)) in your
11
- graphcommerce.config.js
10
+ 1. Only Magento 2.4.6 and earlier, 2.4.7: Configure the following
11
+ ([configuration values](./Config.graphqls)) in your graphcommerce.config.js
12
12
 
13
13
  2. Add `X-Recaptcha` header to your `.meshrc.yml` if it isn't there.
14
14
  [example](../../examples/magento-graphcms/.meshrc.yml)
@@ -0,0 +1,5 @@
1
+ query RecaptchaV3Config {
2
+ recaptchaV3Config {
3
+ website_key
4
+ }
5
+ }
@@ -1,6 +1,7 @@
1
1
  import type { GraphQLRequest } from '@graphcommerce/graphql'
2
2
  import { setContext } from '@graphcommerce/graphql/apollo'
3
3
  import { Kind, OperationTypeNode } from 'graphql'
4
+ import { RecaptchaV3ConfigDocument } from '../graphql/RecaptchaV3Config.gql'
4
5
 
5
6
  const isMutation = (operation: GraphQLRequest) =>
6
7
  operation.query.definitions.some(
@@ -11,8 +12,10 @@ const isMutation = (operation: GraphQLRequest) =>
11
12
 
12
13
  /** Apollo link that adds the Google reCAPTCHA token to the request context. */
13
14
  export const recaptchaLink = setContext(async (operation, context) => {
14
- const recaptchaKey = import.meta.graphCommerce.googleRecaptchaKey
15
- if (!recaptchaKey || !globalThis.grecaptcha || !isMutation(operation)) return context
15
+ const siteKey = context.cache?.readQuery({ query: RecaptchaV3ConfigDocument })?.recaptchaV3Config
16
+ ?.website_key
17
+
18
+ if (!siteKey || !globalThis.grecaptcha || !isMutation(operation)) return context
16
19
 
17
20
  await new Promise<void>((resolve) => {
18
21
  globalThis.grecaptcha?.ready(resolve)
@@ -23,7 +26,7 @@ export const recaptchaLink = setContext(async (operation, context) => {
23
26
  while (failure < 5) {
24
27
  try {
25
28
  // eslint-disable-next-line no-await-in-loop
26
- token = await globalThis.grecaptcha.execute(recaptchaKey, { action: 'submit' })
29
+ token = await globalThis.grecaptcha.execute(siteKey, { action: 'submit' })
27
30
  break
28
31
  } catch {
29
32
  failure++
@@ -0,0 +1,31 @@
1
+ import { storefrontFromContext, type Resolvers } from '@graphcommerce/graphql-mesh'
2
+
3
+ export const resolvers: Resolvers = {
4
+ Query: {
5
+ recaptchaV3Config: (root, args, context, info) => {
6
+ const config = storefrontFromContext(context)
7
+ const key = config?.googleRecaptchaKey ?? import.meta.graphCommerce.googleRecaptchaKey
8
+
9
+ if (!key) return null
10
+ return {
11
+ website_key: key,
12
+ minimum_score: 0,
13
+ badge_position: '',
14
+ failure_message: '',
15
+ is_enabled: !!key,
16
+ forms: [
17
+ 'PLACE_ORDER',
18
+ 'CONTACT',
19
+ 'CUSTOMER_LOGIN',
20
+ 'CUSTOMER_FORGOT_PASSWORD',
21
+ 'CUSTOMER_CREATE',
22
+ 'CUSTOMER_EDIT',
23
+ 'NEWSLETTER',
24
+ 'PRODUCT_REVIEW',
25
+ 'SENDFRIEND',
26
+ 'BRAINTREE',
27
+ ],
28
+ }
29
+ },
30
+ },
31
+ }
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": "9.1.0-canary.16",
5
+ "version": "9.1.0-canary.18",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -15,15 +15,19 @@
15
15
  "@types/grecaptcha": "^3.0.9"
16
16
  },
17
17
  "peerDependencies": {
18
- "@graphcommerce/eslint-config-pwa": "^9.1.0-canary.16",
19
- "@graphcommerce/graphql": "^9.1.0-canary.16",
20
- "@graphcommerce/next-ui": "^9.1.0-canary.16",
21
- "@graphcommerce/prettier-config-pwa": "^9.1.0-canary.16",
22
- "@graphcommerce/typescript-config-pwa": "^9.1.0-canary.16",
18
+ "@graphcommerce/eslint-config-pwa": "^9.1.0-canary.18",
19
+ "@graphcommerce/graphql": "^9.1.0-canary.18",
20
+ "@graphcommerce/graphql-mesh": "^9.1.0-canary.18",
21
+ "@graphcommerce/next-ui": "^9.1.0-canary.18",
22
+ "@graphcommerce/prettier-config-pwa": "^9.1.0-canary.18",
23
+ "@graphcommerce/typescript-config-pwa": "^9.1.0-canary.18",
23
24
  "@mui/material": "^5.10.16",
24
25
  "graphql": "^16.9.0",
25
26
  "next": "*",
26
27
  "react": "^18.2.0",
27
28
  "react-dom": "^18.2.0"
29
+ },
30
+ "dependencies": {
31
+ "@types/react-recaptcha-v3": "^1.1.5"
28
32
  }
29
33
  }
@@ -5,7 +5,6 @@ import { useGoogleRecaptcha } from '../hooks/useGoogleRecaptcha'
5
5
  export const config: PluginConfig = {
6
6
  type: 'component',
7
7
  module: '@graphcommerce/ecommerce-ui',
8
- ifConfig: 'googleRecaptchaKey',
9
8
  }
10
9
 
11
10
  export function ApolloErrorAlert(props: PluginProps<ApolloErrorAlertProps>) {
@@ -5,7 +5,6 @@ import { useGoogleRecaptcha } from '../hooks/useGoogleRecaptcha'
5
5
  export const config: PluginConfig = {
6
6
  type: 'component',
7
7
  module: '@graphcommerce/ecommerce-ui',
8
- ifConfig: 'googleRecaptchaKey',
9
8
  }
10
9
 
11
10
  export function ApolloErrorFullPage(props: PluginProps<ApolloErrorFullPageProps>) {
@@ -5,7 +5,6 @@ import { useGoogleRecaptcha } from '../hooks/useGoogleRecaptcha'
5
5
  export const config: PluginConfig = {
6
6
  type: 'component',
7
7
  module: '@graphcommerce/ecommerce-ui',
8
- ifConfig: 'googleRecaptchaKey',
9
8
  }
10
9
 
11
10
  export function ApolloErrorSnackbar(props: PluginProps<ApolloErrorSnackbarProps>) {
@@ -1,24 +1,37 @@
1
- import type { GraphQLProviderProps } from '@graphcommerce/graphql'
1
+ import { useQuery, type GraphQLProviderProps } from '@graphcommerce/graphql'
2
2
  import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
3
3
  import { GlobalStyles } from '@mui/material'
4
4
  import Script from 'next/script'
5
5
  import { useMemo, useState } from 'react'
6
6
  import type { RecaptchaContext } from '../context/recaptchaContext'
7
7
  import { recaptchaContext } from '../context/recaptchaContext'
8
- import { useGoogleRecaptchaSiteKey } from '../hooks/useGoogleRecaptchaSiteKey'
8
+ import { RecaptchaV3ConfigDocument } from '../graphql/RecaptchaV3Config.gql'
9
9
  import { recaptchaLink } from '../link/recaptchaLink'
10
10
 
11
11
  export const config: PluginConfig = {
12
12
  type: 'component',
13
13
  module: '@graphcommerce/graphql',
14
- ifConfig: 'googleRecaptchaKey',
14
+ }
15
+
16
+ function ReCaptchaScript() {
17
+ const siteKey = useQuery(RecaptchaV3ConfigDocument).data?.recaptchaV3Config?.website_key
18
+
19
+ console.log(siteKey)
20
+
21
+ if (!siteKey) return null
22
+ return (
23
+ <Script
24
+ key='@graphcommerce/grecaptcha'
25
+ strategy='lazyOnload'
26
+ src={`https://www.google.com/recaptcha/api.js?render=${siteKey}`}
27
+ />
28
+ )
15
29
  }
16
30
 
17
31
  export function GraphQLProvider(props: PluginProps<GraphQLProviderProps>) {
18
- const { Prev, links = [], ...prev } = props
32
+ const { Prev, links = [], children, ...prev } = props
19
33
 
20
34
  const [enabled, setEnabled] = useState(false)
21
- const siteKey = useGoogleRecaptchaSiteKey()
22
35
 
23
36
  const context: RecaptchaContext = useMemo(
24
37
  () => ({ enabled, enable: () => setEnabled(true) }),
@@ -27,21 +40,15 @@ export function GraphQLProvider(props: PluginProps<GraphQLProviderProps>) {
27
40
 
28
41
  return (
29
42
  <recaptchaContext.Provider value={context}>
30
- {enabled && (
31
- <>
32
- <Script
33
- key='@graphcommerce/grecaptcha'
34
- strategy='lazyOnload'
35
- src={`https://www.google.com/recaptcha/api.js?render=${siteKey}`}
36
- />
37
- <GlobalStyles styles={{ '.grecaptcha-badge': { visibility: 'hidden' } }} />
38
- </>
39
- )}
40
43
  <Prev
41
44
  {...prev}
42
45
  // Add recaptcha headers to the request.
43
46
  links={[...links, recaptchaLink]}
44
- />
47
+ >
48
+ {children}
49
+ {enabled && <ReCaptchaScript />}
50
+ {enabled && <GlobalStyles styles={{ '.grecaptcha-badge': { visibility: 'hidden' } }} />}
51
+ </Prev>
45
52
  </recaptchaContext.Provider>
46
53
  )
47
54
  }
@@ -0,0 +1,26 @@
1
+ import type { meshConfig as meshConfigBase } from '@graphcommerce/graphql-mesh/meshConfig'
2
+ import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
+
4
+ export const config: PluginConfig = {
5
+ module: '@graphcommerce/graphql-mesh/meshConfig',
6
+ type: 'function',
7
+ }
8
+
9
+ export const meshConfig: FunctionPlugin<typeof meshConfigBase> = (
10
+ prev,
11
+ baseConfig,
12
+ graphCommerceConfig,
13
+ ) => {
14
+ if (graphCommerceConfig.magentoVersion <= 247 || !graphCommerceConfig.googleRecaptchaKey)
15
+ return prev(baseConfig, graphCommerceConfig)
16
+ return prev(
17
+ {
18
+ ...baseConfig,
19
+ additionalResolvers: [
20
+ ...(baseConfig.additionalResolvers ?? []),
21
+ '@graphcommerce/googlerecaptcha/mesh/resolvers.ts',
22
+ ],
23
+ },
24
+ graphCommerceConfig,
25
+ )
26
+ }
@@ -1,8 +0,0 @@
1
- import { useStorefrontConfig } from '@graphcommerce/next-ui'
2
-
3
- export function useGoogleRecaptchaSiteKey() {
4
- const key =
5
- useStorefrontConfig().googleRecaptchaKey ?? import.meta.graphCommerce.googleRecaptchaKey
6
- if (!key) throw new Error('[@graphcommerce/googlerecaptcha]: googleRecaptchaKey not configured')
7
- return key
8
- }