Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Commit b4a01f6

Browse files
authored
Merge pull request redpwn#548 from redpwn/fix/double-recaptcha
fix(recaptcha): prevent double recaptcha initialization
2 parents 529d02f + 462a0f4 commit b4a01f6

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

client/src/components/recaptcha.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const loadRecaptchaScript = () => new Promise((resolve, reject) => {
1515
const recaptchaQueue = []
1616
let recaptchaPromise
1717
let recaptchaId
18+
const handleRecaptchaNext = async () => {
19+
if (recaptchaQueue.length === 0) {
20+
return
21+
}
22+
(await loadRecaptcha()).execute(recaptchaId)
23+
}
1824
const handleRecaptchaDone = async (code) => {
1925
(await loadRecaptcha()).reset(recaptchaId)
2026
const { resolve } = recaptchaQueue.shift()
@@ -27,24 +33,14 @@ const handleRecaptchaError = async (err) => {
2733
reject(err)
2834
handleRecaptchaNext()
2935
}
30-
const handleRecaptchaNext = async () => {
31-
if (recaptchaQueue.length === 0) {
32-
return
33-
}
34-
(await loadRecaptcha()).execute(recaptchaId)
35-
}
3636
const loadRecaptcha = async () => {
37-
if (!recaptchaPromise) {
38-
recaptchaPromise = loadRecaptchaScript()
39-
}
40-
if (!recaptchaId) {
41-
recaptchaId = (await recaptchaPromise).render({
42-
theme: 'dark',
43-
sitekey: config.recaptcha.siteKey,
44-
callback: handleRecaptchaDone,
45-
'error-callback': handleRecaptchaError
46-
})
47-
}
37+
recaptchaPromise = recaptchaPromise ?? loadRecaptchaScript()
38+
recaptchaId = recaptchaId ?? (await recaptchaPromise).render({
39+
theme: 'dark',
40+
sitekey: config.recaptcha.siteKey,
41+
callback: handleRecaptchaDone,
42+
'error-callback': handleRecaptchaError
43+
})
4844
return recaptchaPromise
4945
}
5046
const requestRecaptchaCode = () => new Promise((resolve, reject) => {
@@ -83,7 +79,7 @@ export default (action) => {
8379
loadRecaptcha()
8480
}
8581
}, [recaptchaEnabled])
86-
const callback = useCallback(requestRecaptchaCode, [recaptchaEnabled])
82+
const callback = useCallback(requestRecaptchaCode, [])
8783
if (recaptchaEnabled) {
8884
return callback
8985
}

client/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export default withStyles({
9393
'@global body': {
9494
overflowX: 'hidden'
9595
},
96+
// we show the google legal notice on each protected form
9697
'@global .grecaptcha-badge': {
97-
// we show the google legal notice on each protected form
9898
visibility: 'hidden'
9999
},
100100
// cirrus makes recaptcha position the modal incorrectly, so we reset it here

0 commit comments

Comments
 (0)