Closing inAppBrowser window at the correct moment

I have created an app using Microsoft Azure AD authentication. Now I want this to be wrapped in a phonegap hynbrid mobile app. So far I have managed to call the authentication event using the inAppBrowser. The user can provide his/her credentials and a token is received. The user is logged in and the homepage is opened. However... I have noticed the home page is not opened in the app but in the inAppBrowser. In other words: the login request window was never closed. This would not be a problem if we weren't using the barcode-scanner widget. So the question is: how to close the inAppBrowser window at the right moment. The procedure is as follows: A button on the phonegap index.html opens the inAppBrowser with url=''. This triggers the event handler in the mendix domain to send a request for a token from Azure. When the user supplies his credentials, a token is returned to a callback event handler in the domain. This event creates a session for the user and opens index3.html page. So far I have tried playing with the event listeners of the inAppBrowser. I have tried to use the loadstop event and have the window close whenever the callback has reached the window. But this closes index3.html and the user is confronted with the login page again. Does anybody have any tips for closing the inAppBrowser properly with above configuration?  
1 answers

Lucky you, we have spent quite some blood, sweat and tears creating this beauty together with Danny Roest from Expert Services:


mxMobile.enableSamlLogin = function() {
    var register = function(){
        var samlLogin = function() {
            console.log("Start SAML login");
            var samlWindow = + "SSO/", "_blank", "location=no,toolbar=no");
            var cb = function(event) {
                if (event.url.indexOf( == 0 && event.url.indexOf("SSO") == -1) {

                    console.log("User redirected to app")
                    //make sure this is only called once
                    samlWindow.removeEventListener("loadstop", cb);
                    console.log("Removed event listener");

                        code: "document.cookie;"
                    }, function(values) {
                        var value = values[0] + ";";
                        value = value.substring("AUTH_TOKEN=".length);
                        var token = value.substring(0, value.indexOf(";"));
                        console.log("token: " + token)
                        //var token = value.substring("AUTH_TOKEN=".length, value.indexOf(";"));
                        window.localStorage.setItem("mx-authtoken", token);

                        console.log("Closing window")

                        if ( {
                        } else {


            samlWindow.addEventListener("loadstop", cb);
        window.dojoConfig.ui.customLoginFn = samlLogin;

mxMobile.waitFor = function(fnCondition, succesCallback, errorCallBack, timeoutMs, interval) {
    var waitTime = 0;
    var id = setInterval(function() {
        waitTime += interval;
        if (waitTime > timeoutMs) {
        if (fnCondition()) {
    }, interval);

mxMobile.waitForDojoConfig = function(fnCallback) {
    mxMobile.waitFor(function() {
        return window.dojoConfig;
    }, function() {
        console.log("dojoConfig available");
    }, function() {
        console.log("dojoConfig not created");
    }, 60000, 10);