mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-25 07:02:49 +00:00
e73ad85d0e
GitOrigin-RevId: 3a7e8350b4d6d62583a3cb87378605f13cde3728
55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import 'abort-controller/polyfill'
|
|
import { postJSON } from '../../infrastructure/fetch-json'
|
|
|
|
const grecaptcha = window.grecaptcha
|
|
|
|
let recaptchaId
|
|
const recaptchaCallbacks = []
|
|
|
|
export async function canSkipCaptcha(email) {
|
|
let timer
|
|
let canSkip
|
|
try {
|
|
const controller = new AbortController()
|
|
const signal = controller.signal
|
|
timer = setTimeout(() => {
|
|
controller.abort()
|
|
}, 1000)
|
|
canSkip = await postJSON('/login/can-skip-captcha', {
|
|
signal,
|
|
body: { email },
|
|
swallowAbortError: false,
|
|
})
|
|
} catch (e) {
|
|
canSkip = false
|
|
} finally {
|
|
clearTimeout(timer)
|
|
}
|
|
return canSkip
|
|
}
|
|
|
|
export async function validateCaptchaV2() {
|
|
if (
|
|
// Detect blocked recaptcha
|
|
typeof grecaptcha === 'undefined' ||
|
|
// Detect stubbed recaptcha
|
|
typeof grecaptcha.render !== 'function' ||
|
|
typeof grecaptcha.execute !== 'function' ||
|
|
typeof grecaptcha.reset !== 'function'
|
|
) {
|
|
return
|
|
}
|
|
if (recaptchaId === undefined) {
|
|
const el = document.getElementById('recaptcha')
|
|
recaptchaId = grecaptcha.render(el, {
|
|
callback: token => {
|
|
recaptchaCallbacks.splice(0).forEach(cb => cb(token))
|
|
grecaptcha.reset(recaptchaId)
|
|
},
|
|
})
|
|
}
|
|
return await new Promise(resolve => {
|
|
recaptchaCallbacks.push(resolve)
|
|
grecaptcha.execute(recaptchaId)
|
|
})
|
|
}
|