validateCaptcha.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* global grecaptcha */
  2. /* eslint-disable
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. */
  11. import App from '../base'
  12. export default App.factory('validateCaptcha', function() {
  13. let _recaptchaCallbacks = []
  14. const onRecaptchaSubmit = function(token) {
  15. for (let cb of _recaptchaCallbacks) {
  16. cb(token)
  17. }
  18. _recaptchaCallbacks = []
  19. }
  20. let recaptchaId = null
  21. const validateCaptcha = (callback, captchaDisabled) => {
  22. if (callback == null) {
  23. callback = function(response) {}
  24. }
  25. if (
  26. typeof grecaptcha === 'undefined' ||
  27. grecaptcha === null ||
  28. captchaDisabled
  29. ) {
  30. return callback()
  31. }
  32. const reset = () => grecaptcha.reset()
  33. _recaptchaCallbacks.push(callback)
  34. _recaptchaCallbacks.push(reset)
  35. if (recaptchaId == null) {
  36. const el = $('#recaptcha')[0]
  37. recaptchaId = grecaptcha.render(el, { callback: onRecaptchaSubmit })
  38. }
  39. return grecaptcha.execute(recaptchaId)
  40. }
  41. return validateCaptcha
  42. })