Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Cookie store and maintaining session

$
0
0

Am using 3.2 SDK, Alloys and Android, to maintain session with my server am getting the cookies from the response and saving it in App properties and whenever a request is thrown am using that stored cookie to pass to my server side. But whenever sometimes when jsessionid changes session getting invalid and tried in browser its longing ever and never sessioned out until I logout. So came across addSystemCookie() and addHttpCookie(). Will these both help in my situation or else I have maintain session as I stated previously. If so is there any better example to go through it. My current way of maintaining

var xhr = Ti.Network.createHTTPClient();
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Cookie", Ti.App.Properties.getString("servercookies"));
xhr.onerror = function(e){
    var error = e.error;
    Ti.API.info(error);
};
xhr.onload = function(e){
    if(e.success){
        var headers = this.getResponseHeader('Set-Cookie');
        if(headers){
            setServerCookie(headers, function(){
                var response = this.responseText;
                if(response){//success}
                else{//success}
            });
        }else{
            var response = this.responseText;
            if(response){//success}
            else{//success}
        }                   
    }else{
        alert('Problem occurs. Please contact our customer care for support.');
    }
};
xhr.open(method, url);
xhr.send(JSON.stringify(params));
 
function setServerCookie(headers){
    var keys = Ti.App.Properties.getString('KEY', null);
    if(keys){
        if(headers.indexOf('KEY') >= 0){
            var key = headers.substr(headers.indexOf('KEY'));
            keys = key.substr(0, key.indexOf(';'));
            Ti.App.Properties.setString('KEY', keys);
        }
    }       
    var JSESSIONID = Ti.App.Properties.getString('JSESSIONID', null);
    if(headers.indexOf('JSESSIONID') >= 0){
        var jsess = headers.substr(headers.indexOf('JSESSIONID'));
        JSESSIONID = jsess.substr(0, jsess.indexOf(';'));
        Ti.App.Properties.setString('JSESSIONID', JSESSIONID);
    }
    var key_pair = (keys) ? keys+';' : '';
    var jsession_pair = (JSESSIONID) ? JSESSIONID : '';
    Ti.App.Properties.setString('servercookies', key_pair+jsession_pair);
    //success
}

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>