Session cookies appear not be set with android/phonegap

I am trying to make my Android 4.0 app accept cookies from a Node.Js express server using mongoose-auth for authentication. When I make a post to my login api I can see the set-cookie header being sent using Poster but i dont think it is being stored. I tried using the jquery cookie plug-in but I receive null when attempting to read it. But when I examine request in the api I am able to access session data. Does any one have any sample code of how they accomplished storing cookies in android using phonegap also check if the cookies are being stored in the app?

Below is my endpoint, I am able to session data from another endpoint.

app.post('/api/login', function(request, response){
User.authenticate(request.body.email, request.body.password, function(err, userdoc)     {
    if (userdoc){
        //console.log(request.session);

        request.session.test = "hope this works";
        User.findOne({email : request.body['email']}, ['name'], function (err, user){
            if(!err){
                console.log("name %s", user);
                request.session.user = user;
                response.json({success:true});
            } else{
                console.log(err);
            }

        });
    }
    else {
        response.json({success:false});
    }
});
});

But when I tried reading the cookie with the jquery plugin I get null.

alert($.cookie('connect.sid'));

Any assistance will be greatly appreciated.

Upvotes: 4

Views: 5377

Answers (2)

MHeiss
MHeiss

Reputation: 107

Serverside Cookies (like Security Tokens) have a HttpOnly flag. Cookies with this flag cannot be accessed by JavaScript (document.cookie) for security reasons.

Upvotes: 3

xEviL
xEviL

Reputation: 154

I believe that the reason why your document.cookie is null is that it has the remote domain name, while your javascript from PhoneGap/Cordova app resides in file:///android_asset/www/

Your ajax request qualifies as cross-domain request.

This is the default cookie behavior.

You can probably access the cookies from ajax request via XHR object and headers from response or you can write proxy page on server that renders cookies into page body that can than be read by PhoneGap/Cordova javascript.

Also check this

Upvotes: 4

Related Questions