Cordova inappbrowser cookies when application is killed

7.1k views Asked by At

In my Cordova Application, I try to open a link with inappbrowser. This link points on a domain who need google authentication before access to the page. If I try to open again the link, the domain doesn't need to authenticate again, the session is ok, this works pretty well.

My issue happened when I kill the application. If I want to go on the link again, Google ask me to authenticate again. And I doesn't want to do that.

Is there a way to save the cookies of the inappbrowser plugin ?

Thanks by advance !

2

There are 2 answers

1
yannick1976 On BEST ANSWER

I had the same problem (on android) and realized the library I am using (ngCordova / cordovaOauth) was removing cache and session data (using window.open(..., ..., 'clearsessioncache=yes,clearcache=yes').

So it works now : the login(s) I use are remembered, and I don't have to re-enter my password again. So the cookies are definitely kept, at least on android.

In case you're interested, here is the relevant code in ngCordova :

var redirect_uri = "http://localhost/callback";
if(options !== undefined) {
    if(options.hasOwnProperty("redirect_uri")) {
        redirect_uri = options.redirect_uri;
    }
}
var browserRef = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' 
    + clientId + '&redirect_uri=' + redirect_uri 
    + '&scope=' + appScope.join(" ")
    + '&prompt=select_account&response_type=token', '_blank', 
    'location=no');

browserRef.addEventListener("loadstart", function(event) {
    if((event.url).indexOf(redirect_uri) === 0) {
      browserRef.removeEventListener("exit",function(event){});
      browserRef.close();
        var callbackResponse = (event.url).split("#")[1];
        var responseParameters = (callbackResponse).split("&");
        var parameterMap = [];
        for(var i = 0; i < responseParameters.length; i++) {
            parameterMap[responseParameters[i].split("=")[0]] = responseParameters[i].split("=")[1];
        }
        if(parameterMap.access_token !== undefined && parameterMap.access_token !== null) {
            deferred.resolve({ access_token: parameterMap.access_token, token_type: parameterMap.token_type, expires_in: parameterMap.expires_in });
        } else {
            deferred.reject("Problem authenticating");
        }
    }
});

browserRef.addEventListener('exit', function(event) {
    deferred.reject("The sign in flow was canceled");
});

I modified it a little to suit my needs. You will find the original here, around line 200 (look for google:).

1
AshBringer On

It's just an idea I wanted to share ( never tested ) :

From the documentation :

var ref = window.open(url, target, options);
  • options

clearcache: set to yes to have the browser's cookie cache cleared before the new window is opened

clearsessioncache: set to yes to have the session cookie cache cleared before the new window is opened

I think it's worth the try to play with these options and see what's happenning.