diff options
| author | fja-odoo <fja@odoo.com> | 2020-04-24 10:50:28 +0000 |
|---|---|---|
| committer | fja-odoo <fja@odoo.com> | 2020-06-05 10:58:50 +0000 |
| commit | 541148613451f4c14a299672d91c4397d8a9f6de (patch) | |
| tree | c798cd359c3960d4ae86ebc9847666a3a30bf4a6 /addons/google_recaptcha/static | |
| parent | fea1706f275e88a0d28dd2776c6aa0dd62c1bf3d (diff) | |
[IMP] google_recaptcha, *: integrate recaptchaV3
* = base_setup, website_form, website_sale, website_crm,
crm_iap_lead_website, website_hr_recruitment, website_mass_mailing
Integrate reCaptchaV3 on website_form submit and website_mass_mailing
subscription.
You can now use ReCaptchaV3 to add reCaptcha verification in any module
using google_recaptcha.
Also added a better error management on the form with custom messages.
task-2217980
closes odoo/odoo#48466
Related: odoo/enterprise#9649
Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
Diffstat (limited to 'addons/google_recaptcha/static')
| -rw-r--r-- | addons/google_recaptcha/static/src/js/recaptcha.js | 60 | ||||
| -rw-r--r-- | addons/google_recaptcha/static/src/scss/recaptcha.scss | 5 | ||||
| -rw-r--r-- | addons/google_recaptcha/static/src/xml/recaptcha.xml | 14 |
3 files changed, 79 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 00000000000..9bab4e7827a --- /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 public key set."), + }; + } + await this._recaptchaReady; + try { + return { + token: await window.grecaptcha.execute(this._publicKey, {action: action}) + }; + } catch (e) { + return { + error: _t("The recaptcha public key is invalid."), + }; + } + }, +}); + +return { + ReCaptcha: ReCaptcha, +}; +}); diff --git a/addons/google_recaptcha/static/src/scss/recaptcha.scss b/addons/google_recaptcha/static/src/scss/recaptcha.scss new file mode 100644 index 00000000000..1603d4749b7 --- /dev/null +++ b/addons/google_recaptcha/static/src/scss/recaptcha.scss @@ -0,0 +1,5 @@ +// Hide google recaptcha V3 don't forget to add legal +// https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed +.grecaptcha-badge { + visibility: hidden; +} diff --git a/addons/google_recaptcha/static/src/xml/recaptcha.xml b/addons/google_recaptcha/static/src/xml/recaptcha.xml new file mode 100644 index 00000000000..4b7c23b4c2f --- /dev/null +++ b/addons/google_recaptcha/static/src/xml/recaptcha.xml @@ -0,0 +1,14 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + +<t t-name="google_recaptcha.recaptcha_legal_terms"> + <small class="o_recaptcha_legal_terms"> + Protected by reCAPTCHA, + <a href="https://policies.google.com/privacy" target="_blank">Privacy Policy</a> + & + <a href="https://policies.google.com/terms" target="_blank">Terms of Service</a> + apply. + </small> +</t> + +</odoo> |
