title: PassportJS: How to get req.user in my views
body: <p>I am using pssportjs with express 2.x and a sessions storage. After a login I am getting req.User only once. As soon as I redirect again <strong>req.User is undefined.</strong></p>
  
  <p>Here is my config:</p>
  
  <pre><code>passport.serializeUser(function(user, done) {
    done(null, user._id);
  });
  
  passport.deserializeUser(function(userId, done) {
    User.findOne({_id: userId} ,function(err, user){
      done(err, user);
    });
  });
  
  // Authentication Strategy
  passport.use(new FacebookStrategy({
      clientID: CONFIG.fb.appId,
      clientSecret: CONFIG.fb.appSecret,
      callbackURL: CONFIG.fb.callbackURL
    },
    function(accessToken, refreshToken, profile, done) {
      // asynchronous verification, for effect...
      process.nextTick(function () {
  
        User.findOne({ 'accounts.uid': profile.id, 'accounts.provider': 'facebook' }, function(err, olduser) {
  
            if(olduser) {
              done(null, olduser);
            } else {
              var newuser       = new User();
              var account       = {provider: "facebook", uid: profile.id};
              newuser.accounts.push(account);
              newuser.firstname = profile.name.givenName;
              newuser.lastname  = profile.name.familyName;
              newuser.email     = "TBD...";
  
              newuser.save(function(err) {
                if(err) { throw err; }
                done(null, newuser);
              });
            }
          });
      });
    }
  ));
  
  var app = module.exports = express.createServer();
  
  // configure express
  app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.compiler({ src : __dirname + '/public', enable: ['less']}));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());
  
    //app.use(express.session({ secret: "keyboard cat" }));
    app.use(express.session({
      secret: "victory cat",
      store: new MongoStore({
        url: CONFIG.dbMongo.url
        })
    }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
  });
  </code></pre>
  
  <p>These are my routes:</p>
  
  <pre><code>app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_status', 'user_photos'] }));
  
  app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/index', failureRedirect: '/' }));
  
  app.get('/index', function(req, res) {
  
              res.render('index.ejs', {
                  siteTitle: "Welcome",
                  siteUrl: CONFIG.server.siteUrl,
                  user: req.user
              });
  });
  </code></pre>
  
  <p>And finally this is how I am trying to access the user object in my view:</p>
  
  <pre><code>&lt;div class="page-header"&gt;
      &lt;h3&gt;&lt;%= siteTitle %&gt;&lt;/h3&gt;
  &lt;/div&gt;  
  
  &lt;% if (!user) { %&gt;
       &lt;p&gt;Not logged in !&lt;/p&gt;
  &lt;% } else { %&gt;
       &lt;p&gt;Hello, &lt;%= user.firstname %&gt; &lt;%= user.lastname %&gt;.&lt;/p&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>After authenticating with facebook my view displays firstname and lastname correctly. After another page request req.User is undefiend (deserializing is not being invoced).</p>
  
  <p>What am I doing wrong?</p>