2022-02-04 09:23:15 +00:00
|
|
|
import 'abort-controller/polyfill'
|
2022-01-26 11:15:19 +00:00
|
|
|
import { postJSON } from '../../infrastructure/fetch-json'
|
|
|
|
|
2021-09-08 09:26:18 +00:00
|
|
|
const grecaptcha = window.grecaptcha
|
|
|
|
|
|
|
|
let recaptchaId
|
|
|
|
const recaptchaCallbacks = []
|
|
|
|
|
2022-01-26 11:15:19 +00:00
|
|
|
export async function canSkipCaptcha(email) {
|
2022-02-04 09:23:15 +00:00
|
|
|
let timer
|
2022-01-26 11:15:19 +00:00
|
|
|
let canSkip
|
|
|
|
try {
|
2022-02-04 09:23:15 +00:00
|
|
|
const controller = new AbortController()
|
|
|
|
const signal = controller.signal
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
controller.abort()
|
|
|
|
}, 1000)
|
2022-01-26 11:15:19 +00:00
|
|
|
canSkip = await postJSON('/login/can-skip-captcha', {
|
|
|
|
signal,
|
|
|
|
body: { email },
|
|
|
|
swallowAbortError: false,
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
canSkip = false
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timer)
|
|
|
|
}
|
|
|
|
return canSkip
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:26:18 +00:00
|
|
|
export async function validateCaptchaV2() {
|
2021-09-15 08:43:04 +00:00
|
|
|
if (
|
|
|
|
// Detect blocked recaptcha
|
|
|
|
typeof grecaptcha === 'undefined' ||
|
|
|
|
// Detect stubbed recaptcha
|
|
|
|
typeof grecaptcha.render !== 'function' ||
|
|
|
|
typeof grecaptcha.execute !== 'function' ||
|
|
|
|
typeof grecaptcha.reset !== 'function'
|
|
|
|
) {
|
2021-09-08 09:26:18 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|