2021-09-08 05:26:18 -04:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import { FetchError, postJSON } from '../../infrastructure/fetch-json'
|
2022-01-26 06:15:19 -05:00
|
|
|
import { canSkipCaptcha, validateCaptchaV2 } from './captcha'
|
2021-09-15 04:42:56 -04:00
|
|
|
import inputValidator from './input-validator'
|
2021-09-27 06:45:04 -04:00
|
|
|
import { disableElement, enableElement } from '../utils/disableElement'
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
// Form helper(s) to handle:
|
|
|
|
// - Attaching to the relevant form elements
|
|
|
|
// - Listening for submit event
|
|
|
|
// - Validating captcha
|
|
|
|
// - Sending fetch request
|
|
|
|
// - Redirect handling
|
|
|
|
// - Showing errors
|
|
|
|
// - Disabled state
|
|
|
|
|
|
|
|
function formSubmitHelper(formEl) {
|
|
|
|
formEl.addEventListener('submit', async e => {
|
|
|
|
e.preventDefault()
|
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
formEl.dispatchEvent(new Event('pending'))
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
const messageBag = []
|
|
|
|
|
|
|
|
try {
|
2022-01-26 06:15:19 -05:00
|
|
|
let data
|
|
|
|
try {
|
|
|
|
const captchaResponse = await validateCaptcha(formEl)
|
|
|
|
data = await sendFormRequest(formEl, captchaResponse)
|
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
e instanceof FetchError &&
|
|
|
|
e.data?.errorReason === 'cannot_verify_user_not_robot'
|
|
|
|
) {
|
|
|
|
// Trigger captcha unconditionally.
|
|
|
|
const captchaResponse = await validateCaptchaV2()
|
2022-01-26 07:56:37 -05:00
|
|
|
if (!captchaResponse) {
|
|
|
|
throw e
|
|
|
|
}
|
2022-01-26 06:15:19 -05:00
|
|
|
data = await sendFormRequest(formEl, captchaResponse)
|
|
|
|
} else {
|
|
|
|
throw e
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 05:26:31 -04:00
|
|
|
formEl.dispatchEvent(new Event('sent'))
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
// Handle redirects
|
2021-09-08 05:26:18 -04:00
|
|
|
if (data.redir) {
|
|
|
|
window.location = data.redir
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show a success message (e.g. used on 2FA page)
|
|
|
|
if (data.message) {
|
|
|
|
messageBag.push({
|
|
|
|
type: 'message',
|
2021-09-23 05:48:42 -04:00
|
|
|
text: data.message.text || data.message,
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
}
|
2022-01-25 05:14:17 -05:00
|
|
|
|
|
|
|
// Let the user re-submit the form.
|
|
|
|
formEl.dispatchEvent(new Event('idle'))
|
2021-09-08 05:26:18 -04:00
|
|
|
} catch (error) {
|
|
|
|
let text = error.message
|
|
|
|
if (error instanceof FetchError) {
|
|
|
|
text = error.getUserFacingMessage()
|
|
|
|
}
|
|
|
|
messageBag.push({
|
|
|
|
type: 'error',
|
2021-09-13 04:13:07 -04:00
|
|
|
key: error.data?.message?.key,
|
2021-09-08 05:26:18 -04:00
|
|
|
text,
|
|
|
|
})
|
|
|
|
|
2022-01-25 05:14:17 -05:00
|
|
|
// Let the user re-submit the form.
|
2021-09-16 04:45:19 -04:00
|
|
|
formEl.dispatchEvent(new Event('idle'))
|
2022-01-25 05:14:17 -05:00
|
|
|
} finally {
|
|
|
|
showMessages(formEl, messageBag)
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function validateCaptcha(formEl) {
|
|
|
|
let captchaResponse
|
|
|
|
if (formEl.hasAttribute('captcha')) {
|
2022-01-26 06:15:19 -05:00
|
|
|
if (
|
|
|
|
formEl.getAttribute('action') === '/login' &&
|
|
|
|
(await canSkipCaptcha(new FormData(formEl).get('email')))
|
|
|
|
) {
|
|
|
|
// The email is present in the deviceHistory, and we can skip the display
|
|
|
|
// of a captcha challenge.
|
|
|
|
// The actual login POST request will be checked against the deviceHistory
|
|
|
|
// again and the server can trigger the display of a captcha if needed by
|
|
|
|
// sending a 400 with errorReason set to 'cannot_verify_user_not_robot'.
|
|
|
|
return ''
|
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
captchaResponse = await validateCaptchaV2()
|
|
|
|
}
|
|
|
|
return captchaResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendFormRequest(formEl, captchaResponse) {
|
|
|
|
const formData = new FormData(formEl)
|
|
|
|
if (captchaResponse) {
|
|
|
|
formData.set('g-recaptcha-response', captchaResponse)
|
|
|
|
}
|
|
|
|
const body = Object.fromEntries(formData.entries())
|
|
|
|
const url = formEl.getAttribute('action')
|
|
|
|
return postJSON(url, { body })
|
|
|
|
}
|
|
|
|
|
|
|
|
function showMessages(formEl, messageBag) {
|
|
|
|
const messagesEl = formEl.querySelector('[data-ol-form-messages]')
|
|
|
|
if (!messagesEl) return
|
|
|
|
|
|
|
|
// Clear content
|
|
|
|
messagesEl.textContent = ''
|
2021-09-13 04:13:07 -04:00
|
|
|
formEl.querySelectorAll('[data-ol-custom-form-message]').forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
2021-09-08 05:26:18 -04:00
|
|
|
|
|
|
|
// Render messages
|
|
|
|
messageBag.forEach(message => {
|
2021-09-13 04:13:07 -04:00
|
|
|
if (message.key) {
|
|
|
|
formEl
|
|
|
|
.querySelectorAll(`[data-ol-custom-form-message="${message.key}"]`)
|
|
|
|
.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:26:18 -04:00
|
|
|
const messageEl = document.createElement('div')
|
|
|
|
messageEl.className = classNames('alert', {
|
|
|
|
'alert-danger': message.type === 'error',
|
|
|
|
'alert-success': message.type !== 'error',
|
|
|
|
})
|
|
|
|
messageEl.textContent = message.text
|
2021-09-16 04:14:41 -04:00
|
|
|
messageEl.setAttribute('aria-live', 'assertive')
|
|
|
|
messageEl.setAttribute(
|
|
|
|
'role',
|
|
|
|
message.type === 'error' ? 'alert' : 'status'
|
|
|
|
)
|
2021-09-08 05:26:18 -04:00
|
|
|
messagesEl.append(messageEl)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:47:09 -04:00
|
|
|
export function inflightHelper(el) {
|
|
|
|
const disabledInflight = el.querySelectorAll('[data-ol-disabled-inflight]')
|
|
|
|
const showWhenNotInflight = el.querySelectorAll('[data-ol-inflight="idle"]')
|
|
|
|
const showWhenInflight = el.querySelectorAll('[data-ol-inflight="pending"]')
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
el.addEventListener('pending', () => {
|
2021-09-27 06:45:04 -04:00
|
|
|
disabledInflight.forEach(disableElement)
|
2021-09-21 09:47:09 -04:00
|
|
|
toggleDisplay(showWhenNotInflight, showWhenInflight)
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
|
|
|
|
2021-09-16 04:45:19 -04:00
|
|
|
el.addEventListener('idle', () => {
|
2021-09-27 06:45:04 -04:00
|
|
|
disabledInflight.forEach(enableElement)
|
2021-09-21 09:47:09 -04:00
|
|
|
toggleDisplay(showWhenInflight, showWhenNotInflight)
|
2021-09-08 05:26:18 -04:00
|
|
|
})
|
2021-09-08 05:26:31 -04:00
|
|
|
}
|
2021-09-08 05:26:18 -04:00
|
|
|
|
2021-09-08 05:26:31 -04:00
|
|
|
function formSentHelper(el) {
|
2021-09-21 09:47:09 -04:00
|
|
|
const showWhenPending = el.querySelectorAll('[data-ol-not-sent]')
|
|
|
|
const showWhenDone = el.querySelectorAll('[data-ol-sent]')
|
|
|
|
if (showWhenDone.length === 0) return
|
2021-09-08 05:26:31 -04:00
|
|
|
|
|
|
|
el.addEventListener('sent', () => {
|
|
|
|
toggleDisplay(showWhenPending, showWhenDone)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function formValidationHelper(el) {
|
|
|
|
el.querySelectorAll('input').forEach(inputEl => {
|
|
|
|
if (
|
|
|
|
inputEl.willValidate &&
|
|
|
|
!inputEl.hasAttribute('data-ol-no-custom-form-validation-messages')
|
|
|
|
) {
|
|
|
|
inputValidator(inputEl)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-29 05:04:58 -04:00
|
|
|
function formAutoSubmitHelper(el) {
|
|
|
|
if (el.hasAttribute('data-ol-auto-submit')) {
|
|
|
|
setTimeout(() => {
|
|
|
|
el.querySelector('[type="submit"]').click()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 05:48:09 -04:00
|
|
|
export function toggleDisplay(hide, show) {
|
2021-09-21 09:47:09 -04:00
|
|
|
hide.forEach(el => {
|
|
|
|
el.hidden = true
|
|
|
|
})
|
|
|
|
show.forEach(el => {
|
|
|
|
el.hidden = false
|
|
|
|
})
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function hydrateAsyncForm(el) {
|
2021-09-08 05:26:18 -04:00
|
|
|
formSubmitHelper(el)
|
2021-09-21 09:47:09 -04:00
|
|
|
inflightHelper(el)
|
2021-09-08 05:26:31 -04:00
|
|
|
formSentHelper(el)
|
2021-09-27 06:45:04 -04:00
|
|
|
formValidationHelper(el)
|
2021-09-29 05:04:58 -04:00
|
|
|
formAutoSubmitHelper(el)
|
2021-09-27 06:45:04 -04:00
|
|
|
}
|
2021-09-15 04:42:56 -04:00
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
function hydrateRegularForm(el) {
|
|
|
|
inflightHelper(el)
|
|
|
|
formValidationHelper(el)
|
|
|
|
|
|
|
|
el.addEventListener('submit', () => {
|
|
|
|
el.dispatchEvent(new Event('pending'))
|
2021-09-15 04:42:56 -04:00
|
|
|
})
|
2021-09-29 05:04:58 -04:00
|
|
|
|
|
|
|
formAutoSubmitHelper(el)
|
2021-09-08 05:26:18 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 06:45:04 -04:00
|
|
|
document.querySelectorAll(`[data-ol-async-form]`).forEach(hydrateAsyncForm)
|
|
|
|
|
|
|
|
document.querySelectorAll(`[data-ol-regular-form]`).forEach(hydrateRegularForm)
|