2018-04-19 01:19:10 +05:30
|
|
|
// @flow
|
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;
|
|
|
|
|
2018-04-19 01:19:10 +05:30
|
|
|
export opaque type CaptchaID = string;
|
|
|
|
|
2016-07-31 19:23:16 +05:30
|
|
|
export default {
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @returns {Promise} - resolves to captchaId
|
|
|
|
*/
|
|
|
|
render(
|
|
|
|
el: HTMLElement,
|
|
|
|
{
|
|
|
|
skin: theme,
|
|
|
|
onSetCode: callback,
|
|
|
|
}: {
|
|
|
|
skin: 'dark' | 'light',
|
|
|
|
onSetCode: string => void,
|
2016-07-31 19:23:16 +05:30
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
): Promise<CaptchaID> {
|
|
|
|
return this.loadApi().then(() =>
|
|
|
|
window.grecaptcha.render(el, {
|
|
|
|
sitekey,
|
|
|
|
theme,
|
|
|
|
callback,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
},
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @param {string} captchaId - captcha id, returned from render promise
|
|
|
|
*/
|
|
|
|
reset(captchaId: CaptchaID) {
|
|
|
|
this.loadApi().then(() => window.grecaptcha.reset(captchaId));
|
|
|
|
},
|
2016-08-14 15:40:59 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @param {stirng} newLang
|
|
|
|
*
|
|
|
|
* @see https://developers.google.com/recaptcha/docs/language
|
|
|
|
*/
|
|
|
|
setLang(newLang: string) {
|
|
|
|
lang = newLang;
|
|
|
|
},
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @param {string} apiKey
|
|
|
|
*
|
|
|
|
* @see http://www.google.com/recaptcha/admin
|
|
|
|
*/
|
|
|
|
setApiKey(apiKey: string) {
|
|
|
|
sitekey = apiKey;
|
|
|
|
},
|
2016-08-05 11:11:33 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
loadApi(): Promise<void> {
|
|
|
|
if (!readyPromise) {
|
|
|
|
readyPromise = Promise.all([
|
|
|
|
new Promise(resolve => {
|
|
|
|
window.onReCaptchaReady = resolve;
|
|
|
|
}),
|
|
|
|
options.get().then(resp => this.setApiKey(resp.reCaptchaPublicKey)),
|
|
|
|
]).then(() => {});
|
2016-07-31 19:23:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
loadScript(
|
|
|
|
`https://recaptcha.net/recaptcha/api.js?onload=onReCaptchaReady&render=explicit&hl=${lang}`,
|
|
|
|
);
|
2016-07-31 19:23:16 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return readyPromise;
|
|
|
|
},
|
2016-08-05 11:11:33 +05:30
|
|
|
};
|