Pergunta

I found a nice article about client side authentication with backbone.js.

I need to access a cookie which is generated from server side.

This leads me to the question how do I access the play cookie where some user information is stored in? I am using the Securesocial framework which stores its information in cookies as well.

Is there any chance to do this or are there any alternatives? I also thought about putting call to my REST api "/user" which is sending me back if the user is logged in and other information. This information is than put into a singleton in backbone so I can access this information very easily. I dont know if this is the best way and of course sending everytime a request for gaining the user information is a little bit overhead.

Background information is: I am using backbone for leaving comments to other users. Therefore I need to know which user is currently logged in and if he has the permisssion to delete his own comments.

The login is handled by play with the typical approach and rendering html. I am using therefore the SocialSecure Module.

Foi útil?

Solução

Reading the cookie value is a very simple process, but I'd recommend using some ready-made plugin to speed things up.

Cookies are stored as key-value pairs, so in order to access the value you want, you need to know the key (aka cookie name).

Since you probably are already using jQuery as one of the libraries with Backbone, a good choice would be the following library: https://github.com/carhartl/jquery-cookie/

It's very lightweight and does exactly what you need. Just include that library after you've included the jQuery library, and after that, accessing the cookie values is just as simple as:

var myCookieValue = $.cookie('cookie_name_here');

After this, it's only matter of interpreting the data stored in that value.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top