aurelia-plugins / aurelia-plugins-google-recaptcha

A Google Recaptcha plugin for Aurelia.
MIT License
5 stars 6 forks source link

callback passed to recaptcha API is null when script is already loaded #1

Closed Jaans closed 7 years ago

Jaans commented 7 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: When using the plugin, the first instance of the recaptcha custom element loads in the Google API into the document - and following that resolving - calls this._initialize() where the loaded Google API is invoked to render the recaptcha. Part of this API invocation is to pass in the function reference for the callback that will receive the solved captcha value.

This works correctly, though only where this is either the first instance of the custom element or where this instance is only shown the first time. A use case for example is a simple scenario where the Aurelia router is used and navigates to a route with the recaptcha on. If the user navigates away to another route and then returns back to the first route (the one that contains the recaptcha), a new instance of the recaptcha custom element is constructed.

The current implementation expects this behavior and actually checks whether the API is already loaded and if so, continues onto the call to this._initialize() function to render the captcha, but the call to this._initialize() is actually too early (because no script loading was required) and therefor @bindable callback hasn't been set by Aurelia data binding yet. This results in a bug with a callback function reference of null being passed to the Google API.

Expected/desired behavior: The subsequent instances of the recaptcha plugin should only call this._initialize() (to render the captcha via the Google API) after all Aurelia bindings have been resolved.

The fix is a simple change that requires adding a bind method to the custom element and instead calling the this._initialize() function from there. E.g.:

bind() { 
    this_initialize() 
}

A pull request will follow shortly.

Jaans commented 7 years ago

Resolved with commit 975bfe4.