title: Why Passport.JS returns empty user object after redirect?
body: <blockquote>
  <h1>What is my goal?</h1>
  <p>I'm creating a website using Express.JS and Node.JS that will have <strong>3 panels</strong>:</p>
  <ul>
  <li>User panel (account settings, contact form, etc.),</li>
  <li>Admin panel (option to reply post from contact form, moderating users, etc.),</li>
  <li>Dashboard (panel for owners, where You can manage website, user and admin accounts, etc.).</li>
  </ul>
  <p>First I wrote a code for <strong>Admin Panel</strong> (login form, main page, full reply system), then I started to creating a Dashboard, so I decided to copy code from Admin panel, because it's working and it's all I want.</p>
  <p>I copied the code, changed the necessary variables, etc. Then came testing part. And here is problem.</p>
  </blockquote>
  <hr />
  <blockquote>
  <h1>What is the problem?</h1>
  <p>As above - I copied the working code and I changed <strong>only</strong> variables like <strong>Passport &gt; Strategy name</strong>, <strong>MongoDB Model</strong> and <strong>password encryption string</strong>.
  Then I tested it, and some error occured - When user provide wrong credentials - all work properly, but if user provide <strong>valid</strong> credentials and click &quot;Submit&quot; button - webiste tries to redirect to dashboard, but returns to login page, because <code>req.isAuthenticated()</code> returns <code>false</code> value.</p>
  </blockquote>
  <hr />
  <blockquote>
  <h1>What I checked?</h1>
  <p>I used <code>console.log(req.user)</code> method on almost every step, and everywhere on login page user object is full of correct data and <code>req.isAuthenticated()</code> returns <code>true</code> value, but when page redirect user to panel (as credentials is correct) - there user object just clears (<code>res.json(req.user)</code> is just empty).</p>
  <p>So I think is a some sort of problem with data sending on page redirect.</p>
  </blockquote>
  <hr />
  <blockquote>
  <h1>My parts of code:</h1>
  <p><em>I don't uploaded full code, because there is most part, that is not necessary for this post.</em></p>
  </blockquote>
  <h3><code>index.js</code> - Main file:</h3>
  <pre class="lang-js prettyprint-override"><code>const main = express()
  const dashboard = express()
  
  main.use(vhost(`dashboard.localhost`, dashboard))
  
  dashboard.use(bodyParser.urlencoded({ extended: true }))
  
  dashboard.use(cookieParser())
  
  dashboard.use(&quot;/&quot;, session({
  
      secret: dashboardSecret,
      resave: false,
      saveUninitialized: false,
  
  }))
  
  require(&quot;./config/passport/dashboard&quot;)(passport)
  dashboard.use(passport.initialize())
  dashboard.use(passport.session())
  
  dashboard.use(&quot;/&quot;, require(&quot;./routes/dashboard.routes&quot;))
  </code></pre>
  <h3><code>dashboard.routes.js</code> - Routing file for dashboard:</h3>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;)
  const passport = require(&quot;passport&quot;)
  
  const router = express.Router()
  
  const panel = require(&quot;./../pages/dashboard/panel&quot;)
  const login = require(&quot;./../pages/dashboard/login&quot;)
  const logout = require(&quot;./../pages/dashboard/logout&quot;)
  
  router.get(&quot;/&quot;, panel)
  
  router.get(&quot;/auth&quot;, login)
  
  router.get(&quot;/logout&quot;, logout)
  
  
  router.post(&quot;/auth&quot;, (req, res, next) =&gt; {
  
      passport.authenticate(&quot;dashboard&quot;, {
  
          successRedirect: &quot;/&quot;,
          failureRedirect: &quot;/auth&quot;,
          successFlash: true,
          failureFlash: true,
  
      })(req, res, next)
  
  })
  
  module.exports = router
  </code></pre>
  <h3><code>dashboard.js</code> - File with Passport Strategy for dashboard:</h3>
  <pre class="lang-js prettyprint-override"><code>const DashboardStrategy = require(&quot;passport-local&quot;).Strategy
  const Cryptr = require(&quot;cryptr&quot;)
  
  const { dashboard} = require(&quot;./../encryptionStrings&quot;)
  
  const cryptr = new Cryptr(dashboard)
  
  const Dashboard = require(&quot;./../../models/Dashboard&quot;)
  
  module.exports = function(passport) {
  
      passport.use(&quot;dashboard&quot;, new DashboardStrategy({
  
          usernameField: &quot;nickname&quot;,
  
      }, (nickname, password, done) =&gt; {
  
          Dashboard.findOne({
  
              nickname: nickname.toLowerCase()
  
          }).then(user =&gt; {
  
              if (!user) return done(null, false)
  
              if (password === cryptr.decrypt(user.password)) {
                  
                  return done(null, user)
  
              } else {
  
                  return done(null, false)
  
              }
  
          })
  
      }))
  
      passport.serializeUser(function(user, done) {
  
          done(null, user.id)
  
      })
  
      passport.deserializeUser(async (id, done) =&gt; {
  
          const user = await Managment.findById(id)
  
          done(null, user)
  
      })
  
  }
  </code></pre>
  <h3><code>Dashboard.js</code> - Account Model for Owners:</h3>
  <pre class="lang-js prettyprint-override"><code>const mongoose = require(&quot;mongoose&quot;)
  
  const DashboardSchema = new mongoose.Schema({
  
      nickname: {
  
          type: String,
          required: true,
          unique: true,
  
      },
  
      password: {
  
          type: String,
          required: true,
  
      }
  
  }, {
  
      collection: &quot;dashboard&quot;
  
  })
  
  const Dashboard = mongoose.model(&quot;Dashboard&quot;, DashboardSchema)
  
  module.exports = Dashboard
  </code></pre>