diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/google_recaptcha/static/src/js/recaptcha.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/google_recaptcha/static/src/js/recaptcha.js')
| -rw-r--r-- | addons/google_recaptcha/static/src/js/recaptcha.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/addons/google_recaptcha/static/src/js/recaptcha.js b/addons/google_recaptcha/static/src/js/recaptcha.js new file mode 100644 index 00000000..b4ae76fd --- /dev/null +++ b/addons/google_recaptcha/static/src/js/recaptcha.js @@ -0,0 +1,60 @@ +odoo.define('google_recaptcha.ReCaptchaV3', function (require) { +"use strict"; + +const ajax = require('web.ajax'); +const Class = require('web.Class'); +const core = require('web.core'); + +const _t = core._t; + +const ReCaptcha = Class.extend({ + /** + * @override + */ + init: function () { + this._publicKey = odoo.reCaptchaPublicKey; + }, + /** + * Loads the recaptcha libraries. + * + * @returns {Promise|boolean} promise if libs are loading else false if the reCaptcha key is empty. + */ + loadLibs: function () { + if (this._publicKey) { + this._recaptchaReady = ajax.loadJS(`https://www.recaptcha.net/recaptcha/api.js?render=${this._publicKey}`) + .then(() => new Promise(resolve => window.grecaptcha.ready(() => resolve()))); + return this._recaptchaReady.then(() => !!document.querySelector('.grecaptcha-badge')); + } + return false; + }, + /** + * Returns an object with the token if reCaptcha call succeeds + * If no key is set an object with a message is returned + * If an error occured an object with the error message is returned + * + * @param {string} action + * @returns {Promise|Object} + */ + getToken: async function (action) { + if (!this._publicKey) { + return { + message: _t("No recaptcha site key set."), + }; + } + await this._recaptchaReady; + try { + return { + token: await window.grecaptcha.execute(this._publicKey, {action: action}) + }; + } catch (e) { + return { + error: _t("The recaptcha site key is invalid."), + }; + } + }, +}); + +return { + ReCaptcha: ReCaptcha, +}; +}); |
