damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

Drupal authenication.

I'm using REST server to authenticate on a drupal site. When I run the service I get back the expected data including session_name and session_id.

If I want to request something back from drupal site as logged in user I'm supposed to send that data in a header as a cookie.

My questions are:

  1. Is it possible to setup a cookie in a mobile app?

  2. If it what would be correct way to do it. I tried:

    var sessName = localStorage.getItem("sessName");
    var sessId = localStorage.getItem("sessId");

    $.cookie(sessName,sessId);

    but in console it shows this error:

    Uncaught TypeError: Object function ( selector, context ) {
    // The jQuery object is actually just the init constructor 'enhanced'
    return new jQuery.fn.init( selector, context, rootjQuery );
    } has no method 'cookie'

    Also even if I made a cookie like that would it be used when I invoke a service ?

    I think you can understand my problem. Thank you in advance.

damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

To answer a part of my question alone. I managed to get cookie to save using

var sessName = localStorage.getItem("sessName");
var sessId = localStorage.getItem("sessId");

$.cookie(sessName,sessId);

It was just a problem that i didnt have jquery.cookie.js library I thought jQuery had cookie function.

Rest of my question still stands.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Drupal authenication cookie

Let me know if this helps: http://web.enavu.com/tutorials/workin...

damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

I alredy managed to write cookies that not the problem anymore. My question is:

If I use that method to write a cooke is the cooke going to be sent when I make a rest service invoke ?

Will cookie be sent in a header?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Drupal authenication cookie

I think the cookie should be sent automatically.

damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

I managed to get it to work. Thank you for your help.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Drupal authenication cookie

Good to hear.. the cookie was included?

damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

Yes it was I had some other problem in the app. I made a new app just for testing that and managed to get it to work. I had to setup a cookie with cookie.js but after that app was using cookie natively. If anyone needs help with this just respond to this tread I should get a message and will probably be able to help.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Drupal authenication cookie

If you have a few minutes to post the code you used, that would be great.

damir
Posts: 0
Joined: Sun Apr 01, 2012 10:08 pm

Drupal authenication cookie

// get sessName and sessId from localstorage that you got from calling your service at least those are the names in Drupal
var sessName = localStorage.getItem("sessName");
var sessId = localStorage.getItem("sessId");

// create a cookie make sure you included jquery.cookie.js in external resources
$.cookie(sessName,sessId);

and thats pretty much it.

you can test to see if you have a cookie

alert($.cookie(sessName));

if that shows expected value then cookie will be sent with a request to rest server

I think that explaining Drupal services is beyond the scope of this post.

Return to “Issues”