title: Why is my express-session not persisting?
body: <p>I'm new to express-session. It's not behaving how I would expect, and I'm wondering if someone can point out why. It's likely a basis user-error.</p>
  <p>I'm building an app that stores training information for a volunteer k-9 search and rescue team. I'm attempting to authenticate a sign-in using PassportJS. I'm storing the data through Mongo Atlas. I'm at the level where I just want to see that it's basically working so that I can build upon it further.</p>
  <p>Here is how I set up the middleware:</p>
  <pre><code>router.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: true
  }))
  </code></pre>
  <p>(I'm not sure if I'm misusing Passport, so I'll show how I'm building that out too):</p>
  <pre><code>router.use(passport.initialize()) 
  
  router.use(passport.session())    
  
  authUser = async (username, password, done) =&gt; {
    
    const returnedUser = await User.findOne({username: username}, (err, user) =&gt; {
      if (err) {
        return err
      } else {
        return user
      }
    })
    .clone()
  
  
    if (!returnedUser) {
      return done(null, false) 
    } else if (returnedUser.password != password) {
      return done(null, false) 
    } else {
      return done(null, returnedUser)
    }
  }
  
  passport.use(new LocalStrategy (authUser))
  
  passport.serializeUser( (userObj, done) =&gt; {
    done(null, userObj)
  })
  
  passport.deserializeUser((userObj, done) =&gt; {
    done (null, userObj )
  })
  </code></pre>
  <p>When I sign in, and <code>console.log()</code> the session...</p>
  <pre><code>router.post('/signIn', passport.authenticate('local', {
    // failureRedirect: 'http://localhost:3000/users/signIn'
    failureMessage: 'failure'
  }), (req, res) =&gt; {
    console.log('req.session:', req.session)
    res.send('finished')
  })
  </code></pre>
  <p>...I get what I would expect in the console:</p>
  <pre><code>req.session: Session {
    cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true },
    passport: {
      user: {
        _id: new ObjectId(&quot;62e5505409d8b098030b70aa&quot;),
        username: 'cap1',
        password: 'America',
        firstName: 'Steve',
        lastName: 'Rogers',
        email: 'cap@notshield.org',
        phoneNumber: 1234567890,
        dateCreated: 2022-07-30T15:37:56.079Z,
        k9s: [Array],
        __v: 0
      }
    }
  }
  </code></pre>
  <p>(I'm aware that I need to encrypt this password, but I'm temporarily keeping it visible while I try to understand it.)</p>
  <p>But, then when I test it to see if it persists...</p>
  <pre><code>router.post('/', async (req, res) =&gt;  {
    console.log(req.session)
    //check all field on the front end
    let postUser = new User({
      &quot;username&quot;: req.body.username,
      // &quot;password&quot;: await hasher(req.query.password, 10),
      &quot;password&quot;: req.body.password,
      &quot;firstName&quot;: req.body.firstName,
      &quot;lastName&quot;: req.body.lastName,
      &quot;email&quot;: req.body.email,
      &quot;phoneNumber&quot;: req.body.phoneNumber,
      &quot;dateCreated&quot;: new Date(),
      //query must must be formatted like
      //&amp;k9s[]=spike&amp;k9s[]=lucey
      &quot;k9s&quot;: req.query.k9s
    })
    postNew(postUser, res)
  })
  </code></pre>
  <p>...The session doesn't still contain the passport information in the console.log():</p>
  <pre><code>Session {
    cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }
  }
  </code></pre>
  <p>(I realize that it doesn't make sense in practice to need session from a sign in to create a new user. I'm just trying to use two functions that I know work to test it out.)</p>
  <p>Why is the session not keeping the Passport JS information between http requests?</p>