PhoneGap InAppBrowser event listener too slow (Android)

We are in the midst of creating a PhoneGap-based app using AngularJS and Ionic.

This app is a store management system which ties in with an existing web app using OAuth2, and we have a problem with Android redirecting after authentication.

An event listener is set up as follows to close the InAppBrowser window either upon successfully connecting or cancelling::

if (runningInCordova) {
    connectWindow.addEventListener('loadstart', function(event) {
        var url = event.url;
        if (url.indexOf("code=") > 0 || url.indexOf("error=") > 0) {

            return callback(url).then(function() {
                connectWindow.close();
            },
            function() {
                connectWindow.close();
            });
        }
    });

For browser testing purposes, a localhost redirect URI is also defined:

http://localhost:8100/oauthcallback.html
On iOS this works fine and the InAppBrowser closes immediately when it should, but on Android there is a delay before this event listener fires. The result is that for about 1 second the following error message is displayed:

Web page not available
The web page at http://localhost:8100/oauthcallback.html could not be loaded as:
net::ERR_CONNECTION_REFUSED
The event listener then fires and the window closes.

Is there any way to make the event listener fire more quickly in order to avoid this error?

Many thanks

Do you have an API for your webapp?
Then you could build a login form directly in your mobile app and send the request with the formdata directly to the “login-function” of your web app.

Hi @samuelbennett,

I had written a tutorial for Oauth with InAppBrowser. When I use it, it performs flawlessly for Android and iOS.

Let me know if that makes a difference.

2 Likes

Thanks so much - this looks like just what I need. I will give it a try and get back to you. :smile:

Hi again,

We tried using your example but unfortunately we still get the exact same issue. What version of the inappbrowser plugin are you using?

Thanks

Hi @samuelbennet,

I am using the latest. In fact, I am using this code in an app I just released to the app store.

What version of Android are you testing on?

In the end it turned out we had to set a different callback URI depending on whether the device was iOS or Android. Thanks so much for your help - we have also made use of your PIN tutorial.

Glad to hear it worked out :smile:

hello i try your example with instagram but i can’t made it
my code

 ref.addEventListener('loadstart', function(event) {
            if((event.url).startsWith("http://localhost/callback")) {

                ref.close();
            }
        });

this is result

i made it with like this

 ref.addEventListener('loadstart', function(event) {
                if ((event.url).indexOf("access_token") > -1){
                    alert(event.url);
                    ref.close();
                }

thank you :slight_smile: