diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-28 12:09:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-28 12:09:07 +0300 |
commit | 263f33af2d69b38023c703d274568e2a6da21dee (patch) | |
tree | ba4eebc77592f74c1022ae59f979dce57badc333 /app/assets/javascripts/captcha | |
parent | fd01ef494eca13de9767e697280c6387f0fbb328 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/captcha')
-rw-r--r-- | app/assets/javascripts/captcha/init_recaptcha_script.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/app/assets/javascripts/captcha/init_recaptcha_script.js b/app/assets/javascripts/captcha/init_recaptcha_script.js new file mode 100644 index 00000000000..adc69862a85 --- /dev/null +++ b/app/assets/javascripts/captcha/init_recaptcha_script.js @@ -0,0 +1,50 @@ +// NOTE: This module will be used in https://gitlab.com/gitlab-org/gitlab/-/merge_requests/52044 +import { memoize } from 'lodash'; + +export const RECAPTCHA_API_URL_PREFIX = 'https://www.google.com/recaptcha/api.js'; +/** + * The name which will be used for the reCAPTCHA script's onload callback + */ +export const RECAPTCHA_ONLOAD_CALLBACK_NAME = 'recaptchaOnloadCallback'; + +/** + * Adds the Google reCAPTCHA script tag to the head of the document, and + * returns a promise of the grecaptcha object + * (https://developers.google.com/recaptcha/docs/display#js_api). + * + * It is memoized, so there will only be one instance of the script tag ever + * added to the document. + * + * See the reCAPTCHA documentation for more details: + * + * https://developers.google.com/recaptcha/docs/display#explicit_render + * + */ +export const initRecaptchaScript = memoize(() => { + /** + * Appends the the reCAPTCHA script tag to the head of document + */ + const appendRecaptchaScript = () => { + const script = document.createElement('script'); + script.src = `${RECAPTCHA_API_URL_PREFIX}?onload=${RECAPTCHA_ONLOAD_CALLBACK_NAME}&render=explicit`; + script.classList.add('js-recaptcha-script'); + document.head.appendChild(script); + }; + + /** + * Returns a Promise which is fulfilled after the reCAPTCHA script is loaded + */ + return new Promise((resolve) => { + window[RECAPTCHA_ONLOAD_CALLBACK_NAME] = resolve; + appendRecaptchaScript(); + }); +}); + +/** + * Clears the cached memoization of the default manager. + * + * This is needed for determinism in tests. + */ +export const clearMemoizeCache = () => { + initRecaptchaScript.cache.clear(); +}; |