Skip to content
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

✨Created the initial <amp-recaptcha-input> 3p Bootstrap Iframe #17747

Merged

Conversation

torch2424
Copy link
Contributor

@torch2424 torch2424 commented Aug 28, 2018

relates to #2273

This creates the initial 3p Bootstrap Iframe for <amp-recaptcha-input>. Here are some things to note:

  • renamed recaptcha-bootstrap.js to just recaptcha.js to be consistent, and work with integration.js easier.
  • Used IframeMessagingClient for messaging, instead of messaging.js

I tested this using a gitignored modified amp-gist component. Also, it is worth noting, this has no tests, as it seems all the 3p API type services don't have tests, only the iframe messaging systems do.

Example

This example was made with a modified version of this PR, and I modified <amp-gist> just to show the recaptcha 3p bootstrap iframe and it works! 😄

screen shot 2018-08-27 at 5 25 58 pm

@torch2424
Copy link
Contributor Author

@zhouyx This is now good to go for review 😄

3p/recaptcha.js Outdated
* @param {function(*)} cb
*/
function getRecaptchaApiJs(global, scriptSource, cb) {
writeScript(global, scriptSource, function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we load the script asynchronously with loadScript method?

Copy link
Contributor

Choose a reason for hiding this comment

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

Completely up to your preference, but I kind of feel it's unnecessary to have the getRecaptchaApiJs method as it is only a wrapper to the writeScript, doesn't do anything extra

* @param {!Window} global
* @param {!Object} data
*/
export function recaptcha(global, data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we move this function to the top? So that I read the most important function at first : ) It's easier for code review. Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done :)


grecaptcha.execute(siteKey, {
action: data.action,
})./*OK*/then(function(token) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel we always use token => {...} in the code base. Not sure if it is a code style thing : ) If so maybe it's worth switching.

Copy link
Contributor Author

@torch2424 torch2424 Aug 31, 2018

Choose a reason for hiding this comment

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

So the reason I haven't been using phat arrow functions is because I noticed the other 3p scripts use the normal function callbacks. I think the reasoning is that the other 3p scripts may be from when we still supported IE11, which does not support the phat arrow ES6 syntax. And since I am assuming we don't have a phat arrow polyfill for the 3p scripts, I think we should stick with this style. 😄 Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good! Thanks for the detailed explanation.

3p/recaptcha.js Outdated
);

let recaptchaApiUrl = 'https://www.google.com/recaptcha/api.js?render=';
const siteKey = data.sitekey || '6LebBGoUAAAAAHbj1oeZMBU_rze_CutlbyzpH8VE';
Copy link
Contributor

Choose a reason for hiding this comment

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

😈

3p/recaptcha.js Outdated
'action',
actionTypeHandler.bind(this, grecaptcha, siteKey)
);
iframeMessagingClient./*OK*/sendMessage('ready');
Copy link
Contributor

Choose a reason for hiding this comment

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

In general I think it's better to add prefix to all messages here. So we never mess up with same names. What about amp-recaptcha-ready?

3p/recaptcha.js Outdated
*/
function actionTypeHandler(grecaptcha, siteKey, data) {
if (!iframeMessagingClient) {
user().error(TAG, 'IframeMessagingClient does not exist.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds like a dev error to me instead

@torch2424
Copy link
Contributor Author

@zhouyx This is good to go for another review 😄

Copy link
Contributor

@zhouyx zhouyx left a comment

Choose a reason for hiding this comment

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

LGTM with two minor comments


grecaptcha.execute(siteKey, {
action: data.action,
})./*OK*/then(function(token) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: add comment that the promise is polyfilled.

3p/recaptcha.js Outdated
}).catch(function(err) {
user().error(TAG, err);
iframeMessagingClient./*OK*/sendMessage(MESSAGE_TAG + 'error', dict({
'error': (err || '').toString(),
Copy link
Contributor

Choose a reason for hiding this comment

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

should we use error.message? Or error.name? What exactly are we expecting here?

@zhouyx
Copy link
Contributor

zhouyx commented Sep 4, 2018

LGTM!

@zhouyx zhouyx merged commit 7885e56 into ampproject:master Sep 4, 2018
@torch2424 torch2424 deleted the amp-recaptcha-input-3p-bootstrap-iframe branch September 4, 2018 20:48
Enriqe pushed a commit to Enriqe/amphtml that referenced this pull request Nov 28, 2018
…oject#17747)

* Started the recaptcha 3p frame

* Got the recaptcha api script loaded and working

* Finished initial implementation of 3p recaptcha

* Handled errors and used existing iframe APIs

* MOved to the iframe messaging client

* Fixed other errors, simply need to handle presubmit

* Fixed other presubmit errors, need to refactor iframe messaging

* Fixed all issues with builds and implementation

* Made requested PR changes

* Made some more PR fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants