Skip to content

Recaptcha initialization #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions ReCaptchaAdminUi/view/adminhtml/layout/recaptcha.xml

This file was deleted.

19 changes: 11 additions & 8 deletions ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,19 @@ define(
$(window).trigger('recaptchaapiready');
}.bind(this);

element = document.createElement('script');
scriptTag = document.getElementsByTagName('script')[0];
if (window.addedScriptTag === undefined) {
Copy link
Contributor

@ihor-sviziev ihor-sviziev Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename variable to make it not too general?

Suggested change
if (window.addedScriptTag === undefined) {
if (window.addedRecaptchaScriptTag === undefined) {

Or maybe even better - just add variable inside this JS file and not leak it to window at all?

Copy link
Contributor Author

@engcom-Foxtrot engcom-Foxtrot Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ihor-sviziev this variable should be global.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why?

Copy link
Contributor

@naydav naydav Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can replace global variable on new shared service.

The reCaptcha component will have dependency (via "import" by path) on this shared service.
Inside of the new service adding script tag will proceed only one time.

Thanks


element.async = true;
element.src = 'https://www.google.com/recaptcha/api.js' +
'?onload=globalOnRecaptchaOnLoadCallback&render=explicit' +
(this.settings.rendering.lang ? '&hl=' + this.settings.rendering.lang : '');
element = document.createElement('script');
scriptTag = document.getElementsByTagName('script')[0];

scriptTag.parentNode.insertBefore(element, scriptTag);
element.async = true;
element.src = 'https://www.google.com/recaptcha/api.js' +
'?onload=globalOnRecaptchaOnLoadCallback&render=explicit';

scriptTag.parentNode.insertBefore(element, scriptTag);

window.addedScriptTag = true;
}
},

/**
Expand Down Expand Up @@ -121,7 +124,7 @@ define(
},
'expired-callback': function () {
me.validateReCaptcha(false);
},
}
},
this.settings.rendering
);
Expand Down
4 changes: 1 addition & 3 deletions ReCaptchaUser/view/adminhtml/templates/recaptcha.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
$config = $block->getCaptchaUiConfig();
$renderingOptions = $config['rendering'] ?? [];
$isInvisible = !empty($config['invisible']);
$languageCode = $config['rendering']['lang'] ?? null;
?>
<div class="admin__field <?= /* @noEscape */ $isInvisible ? 'field-invisible-recaptcha' : 'field-recaptcha' ?>">
<div id="admin-recaptcha"
Expand All @@ -25,8 +24,7 @@ $languageCode = $config['rendering']['lang'] ?? null;

element.async = true;
element.src = 'https://www.google.com/recaptcha/api.js'
+ '?onload=globalOnRecaptchaOnLoadCallback&render=explicit'
<?php if (null !== $languageCode): ?><?="+'&hl={$languageCode}'" ?><?php endif; ?>;
+ '?onload=globalOnRecaptchaOnLoadCallback&render=explicit';

window.globalOnRecaptchaOnLoadCallback = function () {
let token = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function get(): array
'sitekey' => $this->getPublicKey(),
'size' => $this->getSize(),
'theme' => $this->getTheme(),
'lang' => $this->getLanguageCode(),
'hl' => $this->getLanguageCode(),
],
'invisible' => false,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get(): array
'sitekey' => $this->getPublicKey(),
'size' => $this->getSize(),
'theme' => $this->getTheme(),
'lang' => $this->getLanguageCode(),
'hl' => $this->getLanguageCode()
],
'invisible' => false,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get(): array
'badge' => $this->getInvisibleBadgePosition(),
'size' => 'invisible',
'theme' => $this->getTheme(),
'lang'=> $this->getLanguageCode()
'hl' => $this->getLanguageCode()
],
'invisible' => true,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function get(): array
'badge' => $this->getInvisibleBadgePosition(),
'size' => 'invisible',
'theme' => $this->getTheme(),
'lang' => $this->getLanguageCode()
'hl' => $this->getLanguageCode()
],
'invisible' => true,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function get(): array
'badge' => $this->getInvisibleBadgePosition(),
'size' => 'invisible',
'theme' => $this->getTheme(),
'lang'=> $this->getLanguageCode()
'hl'=> $this->getLanguageCode()
],
'invisible' => true,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function get(): array
'badge' => $this->getInvisibleBadgePosition(),
'size' => 'invisible',
'theme' => $this->getTheme(),
'lang'=> $this->getLanguageCode()
'hl' => $this->getLanguageCode()
],
'invisible' => true,
];
Expand Down