title: req.user undefined using Express & Node & Passport
body: <p>I am currently creating a small user authentication app using Node + Express + Passport. When the user logs in, they are rerouted automatically to the index page "/" and a session <strong>should</strong> be established with passports authentication. For some reason when trying to <code>console.log(req.user)</code>, it is returning "undefined".</p>
  
  <p>The authentication with passport seems to be working properly with the post route</p>
  
  <pre><code>app.post("/login", passport.authenticate("local", {
      successRedirect: "/home",
      failureRedirect: "/login"
  }), (req, res) =&gt; {
  })
  </code></pre>
  
  <p>But the session is not being established with the user model. I'd like to eventually store the userId in the session. Here is a look at my current set up with user model and passport implementation on the server file.</p>
  
  <pre><code>const mongoose = require("mongoose");
  const passportLocalMongoose = require('passport-local-mongoose');
  
  const userSchema = mongoose.Schema({
      username: String,
      email: String,
      password: String
  });
  
  userSchema.plugin(passportLocalMongoose);
  const user = mongoose.model("User", userSchema);
  
  module.exports = user;
  
  -----------------------------------------------------------------------------------------
  
  const express = require("express"),
          mongoose = require("mongoose"),
          bodyParser = require("body-parser"),
          session = require("express-session"),
          User = require("./models/user"),
          passport = require('passport'),
          LocalStragety = require('passport-local'),
          app = express();
  
  mongoose.connect("mongodb://localhost/shopping_cart_app", { useNewUrlParser: true })
          .then(console.log("MongoDB Connected"))
          .catch(err =&gt; console.log(err));
  
  app.set("view engine", "ejs");
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(express.static(__dirname + '/views'));
  app.use(session({
          secret: "secret",
          resave: false,
          saveUninitialized: true,
          cookie: { secure: true }
  }));
  app.use(passport.initialize());
  app.use(passport.session());
  passport.use(new LocalStragety(User.authenticate()));
  passport.serializeUser(User.serializeUser());
  passport.deserializeUser(User.deserializeUser());
  
  app.post("/login", passport.authenticate("local", {
          successRedirect: "/home",
          failureRedirect: "/login"
  }), (req, res) =&gt; {
  })
  
  </code></pre>
  
  <p>I've tried looking into Passports config a bit more but on the documentation provided, it states that once <code>passport.authenticate</code> runs, a session with the user is established. Any tips would be greatly appreciate.</p>
  
  <p>Thanks</p>