2016-07-31 19:23:16 +05:30
|
|
|
import { loadScript } from 'functions';
|
2016-08-05 11:11:33 +05:30
|
|
|
import options from 'services/api/options';
|
2016-07-31 19:23:16 +05:30
|
|
|
|
|
|
|
let readyPromise;
|
|
|
|
let lang = 'en';
|
|
|
|
let sitekey;
|
|
|
|
|
|
|
|
export default {
|
|
|
|
/**
|
|
|
|
* @param {DOMNode|string} el - dom node or id of element where to render captcha
|
|
|
|
* @param {string} options.skin - skin color (dark|light)
|
|
|
|
* @param {function} options.onSetCode - the callback, that will be called with
|
|
|
|
* captcha verification code, after user successfully solves captcha
|
|
|
|
*
|
2016-08-14 15:40:59 +05:30
|
|
|
* @return {Promise} - resolves to captchaId
|
2016-07-31 19:23:16 +05:30
|
|
|
*/
|
|
|
|
render(el, {skin: theme, onSetCode: callback}) {
|
2016-08-05 11:11:33 +05:30
|
|
|
return this.loadApi().then(() =>
|
2016-07-31 19:23:16 +05:30
|
|
|
window.grecaptcha.render(el, {
|
|
|
|
sitekey,
|
|
|
|
theme,
|
|
|
|
callback
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-08-14 15:40:59 +05:30
|
|
|
/**
|
|
|
|
* @param {string} captchaId - captcha id, returned from render promise
|
|
|
|
*/
|
|
|
|
reset(captchaId) {
|
|
|
|
this.loadApi().then(() => window.grecaptcha.reset(captchaId));
|
|
|
|
},
|
|
|
|
|
2016-07-31 19:23:16 +05:30
|
|
|
/**
|
|
|
|
* @param {stirng} newLang
|
|
|
|
*
|
|
|
|
* @see https://developers.google.com/recaptcha/docs/language
|
|
|
|
*/
|
|
|
|
setLang(newLang) {
|
|
|
|
lang = newLang;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} apiKey
|
|
|
|
*
|
|
|
|
* @see http://www.google.com/recaptcha/admin
|
|
|
|
*/
|
|
|
|
setApiKey(apiKey) {
|
|
|
|
sitekey = apiKey;
|
2016-08-05 11:11:33 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
loadApi() {
|
|
|
|
if (!readyPromise) {
|
|
|
|
readyPromise = Promise.all([
|
|
|
|
new Promise((resolve) => {
|
|
|
|
window.onReCaptchaReady = resolve;
|
|
|
|
}),
|
|
|
|
options.get().then((resp) => this.setApiKey(resp.reCaptchaPublicKey))
|
|
|
|
]);
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2018-03-16 21:35:39 +05:30
|
|
|
loadScript(`https://recaptcha.net/recaptcha/api.js?onload=onReCaptchaReady&render=explicit&hl=${lang}`);
|
2016-08-05 11:11:33 +05:30
|
|
|
}
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2016-08-05 11:11:33 +05:30
|
|
|
return readyPromise;
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|
2016-08-05 11:11:33 +05:30
|
|
|
};
|
2016-07-31 19:23:16 +05:30
|
|
|
|