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 in req.session
, my app doesn'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:
a "check login" function:
var checkLogin = function(req, res, callback) {
if (req.session.user) {
callback();
} else {
res.send({error: "AuthRequired"});
}
};
Error when Ember makes a request:
TypeError: Cannot read property 'user' of undefined
Middleware setup:
app.configure(function () {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/static/'));
app.use(express.cookieParser(<SECRET>));
app.use(express.session());
//to not care about invalid JSON requests
app.use(function(err, req, res, next) {
if (err.message == 'invalid json') {
next()
} else {
next(err)
}
});
});