title: Meteor cookies and Server side routes
body: <p>It seems that in Meteor, we cannot call a server side route to render a file to the page without some sort of work-around from our normal workflow in terms of authentication.</p>
  <h2>Software/Versions</h2>
  <p>I'm using the latest Iron Router 1.* and Meteor 1.* and to begin, I'm just using accounts-password.</p>
  <h2>Background/Context</h2>
  <p>I have an onBeforeAction that simply redirects the user to either the welcome page or home page base upon if the user is logged in or not:</p>
  <p>both/routes.js</p>
  <pre><code>if (Meteor.isClient) {
  Router.onBeforeAction(function () {
    if (!Meteor.user() || Meteor.loggingIn())
      this.redirect('welcome.view');
    else
      this.next();
    }
    ,{except: 'welcome.view'}
  );
  
  Router.onBeforeAction(function () {
    if (Meteor.user())
      this.redirect('home.view');
    else
      this.next();
    }
    ,{only: 'welcome.view'}
  );
  }
  </code></pre>
  <p>In the same file, both/routes.js, I have a simple server side route that renders a pdf to the screen:</p>
  <pre><code>Router.route('/pdf-server', function() {
    var filePath = process.env.PWD + &quot;/server/.files/users/test.pdf&quot;;
    console.log(filePath);
    var fs = Npm.require('fs');
    var data = fs.readFileSync(filePath);
    this.response.write(data);
    this.response.end();
  }, {where: 'server'});
  </code></pre>
  <h2>Cookie related code added</h2>
  <p>I found a SO answer where a method to set and get the cookies is outlined here: <a href="https://stackoverflow.com/questions/16727922/access-meteor-userid-from-outside-a-method-publish">SO Cookies technique</a></p>
  <p>And then added the following code to my project:
  client/main.js</p>
  <pre><code>Deps.autorun(function() {
      if(Accounts.loginServicesConfigured() &amp;&amp; Meteor.userId()) {
          setCookie(&quot;meteor_userid&quot;,Meteor.userId(),30);
          setCookie(&quot;meteor_logintoken&quot;,localStorage.getItem(&quot;Meteor.loginToken&quot;),30);
      }
  }); 
  </code></pre>
  <p>But this does not work as expected, the setCookie code is not valid for some reason.</p>
  <h2>My Questions</h2>
  <blockquote>
  <p>Question 1: Using cookies, how do I properly set/get and check for the cookies?
  Question 2: What precautions should I take using the cookies approach?</p>
  <p>Side Question: Is cookies the best way to go about this, or is there a
  simpler way to achieve the same thing?</p>
  </blockquote>