title: req.user is not being set to res.locals on app.use in express passport node js
body: <p>I'm new to nodejs, I'm trying to develop a website with two users, a normal user, and a freelancer. When the user logs in <code>res.locals.currentUser</code> is set as the <code>req.user</code> by using the middleware <code>app.use</code> (I'm using the same middle-ware for both types of user).</p>
  
  <pre><code>app.use(session({
  secret: "Rusty is the best and cutest dog in the world",
  resave: false,
  saveUninitialized: false,
  store: new MongoStore({ mongooseConnection: mongoose.connection }),
  cookie: { maxAge: 180 * 60 *1000 }
  }));
  </code></pre>
  
  <p>My <code>app.use</code> code is </p>
  
  <pre><code>app.use(passport.initialize());
  app.use(passport.session());
  
  app.use(function(req, res, next){
  console.log("inside app use");
  res.locals.currentUser = req.user;
  res.locals.session = req.session;
  console.log(req.user);
  next();
  });
  </code></pre>
  
  <p>When the user logsin req.user shows the right user but when the freelancer logsin req.user here inside app.use is undefined. </p>
  
  <p>My passport configuration for the user is </p>
  
  <pre><code>passport.use('user', new LocalStrategy(User.authenticate()));
  passport.serializeUser(User.serializeUser());
  passport.deserializeUser(User.deserializeUser());
  </code></pre>
  
  <p>My passport configuration for the admin is</p>
  
  <pre><code>passport.use('freelancer', new LocalStrategy(Freelancer.authenticate()));
  passport.serializeUser(Freelancer.serializeUser());
  passport.deserializeUser(Freelancer.deserializeUser());
  </code></pre>
  
  <p>The user login which works fine is </p>
  
  <pre><code>app.post("/user-login", passport.authenticate("user", {
  
  failureRedirect: "/user-login"
  }) ,function(req, res){
  console.log("success");
  if(req.session.oldUrl){
      var oldUrl = req.session.oldUrl;
      req.session.oldUrl = null;
      res.redirect(oldUrl);
  
  }else{
      console.log("inside else");
      res.redirect("/my-user-profile/"+req.user._id);
  }
  });
  </code></pre>
  
  <p>The problem is with the other type of user the Freelancer.
  My freelancer login route is</p>
  
  <pre><code>app.post("/freelancer-login", passport.authenticate("freelancer", {
  failureRedirect: "/freelancer-login"
  }) ,function(req, res){
  res.locals.currentUser = req.user;
  res.locals.session = req.session;
  console.log("currentUser ------&gt;");
  console.log(res.locals.currentUser);
  res.redirect("f-profile/"+req.user._id);
  });
  </code></pre>
  
  <p>on my console.log(res.locals.currentUser); Im getting the correct information of the user and also the req.user shows the correct information inside this route but when Im trying to access currentUser in other ejs views , currentUser is undefined just for the freelancer 
  The current user inside app.use also shows undefined (just for the freelancer).</p>
  
  <p>I've been stuck at this for a long time and any kind of help would be a lifesaver</p>
  
  <p>Thanks in advance :-)</p>