Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get cookie back from Cordova InAppBrowser in Ionic 2

I am building a mobile version of an web application with Ionic 2 that uses SAML for SSO that is ran on my client's server. Right now we have an api that gets called when you are not logged into the website that redirects to their server for SSO, then returns back to our server when you log in. Then, the browser has cookie to store that the user is logged in.

I need to take that cookie and somehow get it to my Ionic application. I know that you can't share cookies from the InAppBrowser back to the application, but there has to be some way to get it back. Enough applications use Twitter, Facebook, etc that I assume there is something simple that I am missing.

like image 721
loganhuskins Avatar asked Jun 08 '16 20:06

loganhuskins


1 Answers

Yeah this is notoriously difficult because of domain policy and inAppBrowser is a new instance that doesn't allow previous web view to access it.

There is a way however using InAppBrowser's executeScript() method that lets you execute JavaScript in the opened window.

var win = window.open( "http://icenium.com", "_blank", "EnableViewPortScale=yes" );
win.addEventListener( "loadstop", function() {
    win.executeScript({ code: "alert( 'hello' );" });
});

You can even provide a callback to retrieve values from the opened window

var win = window.open( "http://icenium.com", "_blank", "EnableViewPortScale=yes" );
win.addEventListener( "loadstop", function() {
    win.executeScript(
        { code: "document.body.innerHTML" },
        function( values ) {
            alert( values[ 0 ] );
        }
    );
});

Then you could use the returned value to save it in local storage

This site provides some great code to help you out.

like image 153
Mark Avatar answered Oct 30 '22 19:10

Mark