title: Passport.js: how to access user object after authentication?
body: <p>I'm using Passport.js to login a user with username and password.  I'm essentially using the sample code from the Passport site.  Here are the relevant parts (I think) of my code:</p>
  
  <pre><code>app.use(passport.initialize());
  app.use(passport.session());
  
  passport.serializeUser(function(user, done) {
      done(null, user);
  });
  
  passport.deserializeUser(function(obj, done) {
      done(null, obj);
  });
  
  passport.use(new LocalStrategy(function(username, password, done) {
      User.findOne({ username: username }, function(err, user) {
          if (err) {
              return done(err);
          }
          if (!user) {
              return done(null, false, { message: 'Incorrect username.' });
          }
          if (!user.validPassword(password)) {
              return done(null, false, { message: 'Incorrect password.' });
          }
          return done(null, user);
          });
      }
  ));
  
  app.post('/login',
      passport.authenticate('local', { failureRedirect: '/login/fail', failureFlash: false }),
      function(req, res) {
          // Successful login
          //console.log("Login successful.");
          // I CAN ACCESS req.user here
  });
  </code></pre>
  
  <p>This seems to login correctly.  However, I would like to be able to access the login user's information in other parts of the code, such as:</p>
  
  <pre><code>app.get('/test', function(req, res){
      // How can I get the user's login info here?
      console.log(req.user);  // &lt;------ this outputs undefined
  });
  </code></pre>
  
  <p>I have checked other questions on SO, but I'm not sure what I'm doing wrong here.  Thank you!</p>