homeflowjs 1.1.8 → 1.2.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.
@@ -1,21 +1,35 @@
1
1
  import { useCallback, useEffect, useState } from 'react';
2
2
 
3
- // Currenly supports only Recaptcha V2
4
-
5
3
  const RECAPTCHA_CONFIG = {
6
- KEY: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
7
- FILED_NAME: 'g-recaptcha-response',
4
+ V2_KEY: '6Lf16S0UAAAAAL0YaWCLRhChd4Uk77b-4Ai0ZdRY',
5
+ V3_KEY: '6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K',
6
+ FIELD_NAME: 'g-recaptcha-response',
8
7
  SCRIPT_ID: 'recaptcha-script',
9
8
  SCRIPT_LINK: 'https://www.google.com/recaptcha/api.js?render=explicit',
9
+ V3_SCRIPT_LINK: 'https://www.google.com/recaptcha/api.js?render=6LeKPOUUAAAAAEKNWIu2qkNwdOqVxH41v0D7fe-K',
10
+ };
11
+
12
+ // default to 3 in all cases for HFJS themes
13
+ // override to 2 in theme if required
14
+ const isRecaptchaV3 = () => {
15
+ const recaptchaVersion = Homeflow.get('recaptcha_version');
16
+
17
+ if (recaptchaVersion === 2) {
18
+ return false;
19
+ }
20
+
21
+ return true;
10
22
  };
11
23
 
24
+
12
25
  const useRecaptcha = ({
13
- apiKey = RECAPTCHA_CONFIG.KEY,
26
+ apiKey = RECAPTCHA_CONFIG.V2_KEY,
14
27
  formID,
15
28
  formRef,
16
29
  onRecaptchaSubmit,
17
30
  shouldKickGAEvent = true,
18
31
  recaptchaSize = 'invisible',
32
+ recaptchaAction = 'submit',
19
33
  }) => {
20
34
  const RECAPTCHA_CONTAINER_ID = `recaptcha-wrapper-${formID}`;
21
35
 
@@ -30,7 +44,9 @@ const useRecaptcha = ({
30
44
  recaptchaScript.onload = resolve;
31
45
  recaptchaScript.onerror = reject;
32
46
  recaptchaScript.id = RECAPTCHA_CONFIG.SCRIPT_ID;
33
- recaptchaScript.src = RECAPTCHA_CONFIG.SCRIPT_LINK;
47
+ recaptchaScript.src = isRecaptchaV3()
48
+ ? RECAPTCHA_CONFIG.V3_SCRIPT_LINK
49
+ : RECAPTCHA_CONFIG.SCRIPT_LINK;
34
50
  document.body.appendChild(recaptchaScript);
35
51
  },
36
52
  );
@@ -53,21 +69,20 @@ const useRecaptcha = ({
53
69
  }
54
70
  }, [stateWidget]);
55
71
 
72
+ const isRecaptchaScriptReady = () => (
73
+ isRecaptchaV3() ? window.grecaptcha?.execute : window.grecaptcha?.render
74
+ );
75
+
56
76
  const waitForRecaptchaScript = () => {
57
77
  const intervalId = setInterval(() => {
58
- if (window.grecaptcha?.render) {
78
+ if (isRecaptchaScriptReady()) {
59
79
  setStateIsRecaptchaLoaded(true);
60
80
  }
61
81
  }, 500);
62
82
  setStateIntervalId(intervalId);
63
83
  };
64
84
 
65
- /*
66
- * has to be rendered on form submit and not earlier
67
- * so that onRecaptchaSubmit function has updated reference
68
- * and uses updated React states
69
- */
70
- const triggerRecaptchaRender = useCallback(() => {
85
+ const triggerRecaptchaRenderV2 = () => {
71
86
  const currentRecaptchaContainer = document.getElementById(RECAPTCHA_CONTAINER_ID);
72
87
 
73
88
  if (!currentRecaptchaContainer) {
@@ -77,12 +92,38 @@ const useRecaptcha = ({
77
92
  const widget = window.grecaptcha.render(`recaptcha-wrapper-${formID}`, {
78
93
  sitekey: apiKey,
79
94
  callback: (token) => onRecaptchaSubmit({
80
- [RECAPTCHA_CONFIG.FILED_NAME]: token
95
+ [RECAPTCHA_CONFIG.FIELD_NAME]: token
81
96
  }),
82
97
  size: recaptchaSize,
83
98
  });
84
99
 
85
100
  setStateWidget(widget);
101
+ };
102
+
103
+ const triggerRecaptchaRenderV3 = () => {
104
+ window.grecaptcha.ready(() => {
105
+ window.grecaptcha.execute(RECAPTCHA_CONFIG.V3_KEY, { action: recaptchaAction })
106
+ .then((token) => {
107
+ onRecaptchaSubmit({ [RECAPTCHA_CONFIG.FIELD_NAME]: token });
108
+
109
+ if (shouldKickGAEvent) {
110
+ Homeflow.kickEvent('after_successful_recaptcha', formRef.current);
111
+ }
112
+ });
113
+ });
114
+ };
115
+
116
+ /*
117
+ * has to be rendered on form submit and not earlier
118
+ * so that onRecaptchaSubmit function has updated reference
119
+ * and uses updated React states
120
+ */
121
+ const triggerRecaptchaRender = useCallback(() => {
122
+ if (isRecaptchaV3()) {
123
+ triggerRecaptchaRenderV3();
124
+ } else {
125
+ triggerRecaptchaRenderV2();
126
+ }
86
127
  }, [onRecaptchaSubmit]);
87
128
 
88
129
  useEffect(() => {
@@ -95,14 +136,14 @@ const useRecaptcha = ({
95
136
  const onRecaptchaRender = useCallback(() => {
96
137
  const recaptchaTokenEl = formRef
97
138
  .current
98
- .querySelector(`[name="${RECAPTCHA_CONFIG.FILED_NAME}"]`);
99
-
139
+ .querySelector(`[name="${RECAPTCHA_CONFIG.FIELD_NAME}"]`);
140
+
100
141
  if (recaptchaTokenEl){
101
142
  onRecaptchaSubmit({
102
- [RECAPTCHA_CONFIG.FILED_NAME]: recaptchaTokenEl.value
143
+ [RECAPTCHA_CONFIG.FIELD_NAME]: recaptchaTokenEl.value
103
144
  })
104
145
  } else {
105
- if (window.grecaptcha?.render) {
146
+ if (isRecaptchaScriptReady()) {
106
147
  triggerRecaptchaRender();
107
148
  } else {
108
149
  waitForRecaptchaScript();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",
@@ -45,7 +45,7 @@ const UserRegisterForm = (props) => {
45
45
  window.location.href = '/';
46
46
  }
47
47
 
48
- createUser(userParams)
48
+ createUser(userParams, recaptchaData)
49
49
  .then((data) => {
50
50
  if (data?.validationErrors?.length > 0) {
51
51
  setValidationErrors(data.validationErrors);
@@ -74,6 +74,7 @@ const UserRegisterForm = (props) => {
74
74
  formID: 'registration-form',
75
75
  formRef,
76
76
  onRecaptchaSubmit,
77
+ recaptchaAction: 'register',
77
78
  });
78
79
 
79
80
  const handleSubmit = (e) => {