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