{"id":3354,"date":"2014-03-23T11:23:00","date_gmt":"2014-03-23T11:23:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/ember-js-node-js-authentication-session-not-working-with-express-session-collection-of-common-programming-errors\/"},"modified":"2014-03-23T11:23:00","modified_gmt":"2014-03-23T11:23:00","slug":"ember-js-node-js-authentication-session-not-working-with-express-session-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/ember-js-node-js-authentication-session-not-working-with-express-session-collection-of-common-programming-errors\/","title":{"rendered":"Ember.js + Node.js Authentication\/Session Not Working with Express.Session-Collection of common programming errors"},"content":{"rendered":"<p>I have been experimenting with Ember.js and Node.js, and have run into a problem. I have protected my API routes on the server with the standard <code>Express.session<\/code> system, but I have run into a problem when those APIs are serving an Ember.js application. Since the Ember app does not have the ability to store values in <code>req.session<\/code>, my app doesn&#8217;t function. How should I do my authentication so that I still can session-protect my API? Here is some of my current server login code:<\/p>\n<p><strong>a &#8220;check login&#8221; function:<\/strong><\/p>\n<pre><code>var checkLogin = function(req, res, callback) {\n    if (req.session.user) {\n      callback();\n    } else {\n      res.send({error: \"AuthRequired\"});\n    }\n  };\n<\/code><\/pre>\n<p><strong>Error when Ember makes a request:<\/strong><\/p>\n<pre><code>TypeError: Cannot read property 'user' of undefined\n<\/code><\/pre>\n<p><strong>Middleware setup:<\/strong><\/p>\n<pre><code>app.configure(function () {\n  app.use(express.bodyParser());\n  app.use(express.static(__dirname + '\/static\/'));\n  app.use(express.cookieParser());\n  app.use(express.session());\n  \/\/to not care about invalid JSON requests\n  app.use(function(err, req, res, next) {\n    if (err.message == 'invalid json')  {\n        next()\n    } else {\n        next(err)\n    }\n  });\n});\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have been experimenting with Ember.js and Node.js, and have run into a problem. I have protected my API routes on the server with the standard Express.session system, but I have run into a problem when those APIs are serving an Ember.js application. Since the Ember app does not have the ability to store values [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3354","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3354"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3354\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}