Unable to access req.user in Passport.js on Express.js
<p><code>req.user</code> not accessible anywhere except its origin function. </p>

<p>I read that passport attaches the user to every request after authentication.</p>

<p>I want to stop any user from accessing the inner pages through the url bar thus skipping the login page or if his session is already active he must be redirected to the user page. These two things can be done only if I get access to <code>req.user</code> in all the middlewares.</p>

<p><a href="https://github.com/ehra/hostelPEB/blob/master/app.js" rel="nofollow">app.js link</a></p>

<p>app.js:-</p>

<pre><code>app.post('/',passport.authenticate('local',{failureRedirect: '/'}),
function(req,res,next){
console.log(req.user); //req.user defined here only not in users.js
res.redirect('/users');
});
</code></pre>

<p>users.js:-</p>

<pre><code>router.get('/users', function(req, res, next) {
console.log(req.user);//undefined    
res.render('users');
});
</code></pre>

<p>DeserializeUser:-</p>

<pre><code>passport.deserializeUser(function(id, done) {
db.findById(id, function(err, user) {
done(err, user);
console.log(user) //undefined 
});
});
</code></pre>