Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set and get cookies (server side) in Meteor?

How can I set and get cookies (server side) in Meteor?

like image 351
Mattias Petter Johansson Avatar asked Apr 11 '12 15:04

Mattias Petter Johansson


People also ask

Can cookies be server side?

There is no difference. A regular cookie can be set server side or client side. The 'classic' cookie will be sent back with each request. A cookie that is set by the server, will be sent to the client in a response.

Is Meteor a server side?

Meteor methods are functions that are written on the server side, but can be called from the client side.

Can you set cookies client side?

Cookies can be set or read server side, or client side.

What are server cookies?

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to a user's web browser. The browser may store the cookie and send it back to the same server with later requests.


2 Answers

Meteor does not currently have a supported way to use cookies on the server.

You can use cookies on the client, though. Here's a snippet to show a splash screen the first time the user visits a page:

Meteor.startup(function () {
    if (!document.cookie.match("splash="))      
      $('body').append(Meteor.ui.render(Template.splash));      
});

Template.splash.events = {
    'click .submit': function () {      
        document.cookie = "splash=ack;expires=Sat, 23 Mar 2013 00:00:0 GMT";    
        $('#splash_outer').remove();        
    }   
};

You could use a similar approach and set the cookies in client side code, then send the results to the server in a method call.

like image 111
n1mmy Avatar answered Oct 12 '22 10:10

n1mmy


It looks like we got a solution: ostrio/cookies which work both on the server side and on the client side: https://atmospherejs.com/ostrio/cookies

import { Cookies } from 'meteor/ostrio:cookies';

const cookies = new Cookies();

const oldValue = cookies.get("key");
cookies.set("key", "newValue");
like image 20
Artem Avatar answered Oct 12 '22 08:10

Artem