Reputation: 492
i'm using express.session() in my express app so i've created a middleware for authentication user before sending them to '/' my code is :
var express = require('express');
app.configure(function(){
app.use(express.cookieParser('keyboard cat'));
app.use(express.session( {secret : "my secret"}));
app.use(app.router);
});
var accessChecker= function(req,res,next){
if (req.session.user.name && req.session.auth){
next();
}else{
res.redirect('/login');
}
}
app.get('/',accessChecker(), routes.index);
When i run this code i get this message :
if (req.session.user.name && req.session.auth){
^
TypeError: Cannot read property 'session' of undefined
i've tried to put accessChecker function before app.configure and to use express.cookieParser() and express.session() inside acessChecker function but always i get the same message !
Upvotes: 1
Views: 1894
Reputation: 64342
You are invoking the accessChecker
function rather passing a pointer to the function. This will fix the problem:
app.get('/', accessChecker, routes.index);
For more examples see the documentation. I'd also suggest looking at passport if you want to do authentication.
Upvotes: 4