title: Get current logged in username using Passport JS?
body: <p>I have created a simple user login application following an online tutorial using Node, Express and Passport. Login works correctly but I would like to get the username of the current logged in user and I can't seem to get this working.</p>
  
  <p>I have the following in my app.js:</p>
  
  <pre><code>/// Configuring Passport
  var passport = require('passport');
  var expressSession = require('express-session');
  
  app.use(expressSession({
    secret: 'cookie_secret',
      name: 'cookie_name',
      proxy: true,
      resave: true,
      saveUninitialized: true
  }));
  
  app.use(passport.initialize());
  app.use(passport.session({
      secret: 'cookie_secret',
      name: 'cookie_name',
      proxy: true,
      resave: true,
      saveUninitialized: true
  }));
  </code></pre>
  
  <p>From what I read in similar posts, I need to expose the username so that I may use it in other files. I tried to do this in my app.js:</p>
  
  <pre><code>app.get('/home', function(req, res) {
    res.render('home.jade', { username: req.user.username });
  });
  </code></pre>
  
  <p>Home is a route where I would like to use the the username, and I am trying to access the username with an alert like the following:</p>
  
  <pre><code>alert(req.user.username);
  </code></pre>
  
  <p>This does not work, the req object is undefined...I'm not sure what I am missing? </p>
  
  <hr>
  
  <p>Managed to get this working. In my app.js I have:</p>
  
  <pre><code>app.get('/home', function(req, res) {
    res.render('home.jade', { username: req.user.username });
  });
  </code></pre>
  
  <p>And then in my /home route I can access the username by doing:</p>
  
  <pre><code>req.user.username
  </code></pre>