Closing inAppBrowser window at the correct moment

1
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='https://our-app.com/signin'. 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?  
asked
1 answers
5

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 = cordova.InAppBrowser.open(window.mx.remoteUrl + "SSO/", "_blank", "location=no,toolbar=no");
            var cb = function(event) {
                if (event.url.indexOf(window.mx.remoteUrl) == 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");

                    samlWindow.executeScript({
                        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")
                        samlWindow.close();

                        if (window.mx.afterLoginAction) {
                            window.mx.afterLoginAction();
                        } else {
                            console.log("startup");
                        }

                    });

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

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

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

 

answered