Using cookies in meteor


#1

Hi I am looking for a cookie type implementation using meteor. Can you pls let me know how to go about it? Or is there any other way similar to cookie implementation in meteor?


#2

What are you trying to do?

If you want to store something on the client, you can just use local storage.

http://dev.w3.org/html5/webstorage/
http://www.html5rocks.com/en/features/storage


#3

what is the best or what is the different between storing in a cookie as opposed to local storage?


#4

all I am trying to do is store a user identification token. So not sure if cookie is the right storage or local storage or session storage.


#5

For that you’d usually use a cookie. For packages that make it really easy to use, look here: https://atmospherejs.com/?q=cookie – the first 4 results all look solid.


#6

Meteor’s Session is lost whenever the user reloads the page, so it’s not persistent at all and mostly only useful for storing state relative to the current UI, which would be lost on page reload anyway.
And HTML5 local storage is a storage option to be aware of and read a bit about. It’s like a client-side persistent cache or session. It’s useful, mostly the use case is “I need to store lots of data purely client-side” which rules out cookies (they have a hard size limit) and makes storing on the server unattractive (beause it just causes unnecessary load).


#7

I put the jquery-cookie script somewhere in the client folder and then stuff like this works just fine:

$.cookie("post_id", post_id, {expires: 365, path: '/'});
$.removeCookie("post_id", {path: '/'});
var cookieValue = $.cookie("post_id"); 

I see some folks have packaged that script up and made it available on atmosphere (https://atmospherejs.com/?q=jquery-cookie). Much better approach than just throwing the script somewhere in the client folder.


#8

I am using the chuangbo cookie but if the do this just to test, I get an error
ReferenceError: Cookie is not defined

Cookie.set(‘tokenId’, ‘1f8fad5b-d9cb-469f-a165-70867728950e’, {
domain: ‘rakshak.meteor.com’,
path: ‘/’,
expires: 30
});

I am doing this in server.js code.


#9

Cookie is used on the client side only