title: Trouble with Passport.js cookies
body: <p>This code describes the REST API that provides session and cookie authentication. The trouble is that the cookie is not being set on the client side, so when the client visits <code>/dashboardData</code>, the server responds with a 401.</p>
  <p>In the network tab of the dev tools, a 302 redirect along with the response cookie is present. However, upon viewing the Storage/Cookies tab of the dev tools, there are no cookies present.</p>
  <p>The following code describes the server.</p>
  <pre><code>const express = require(&quot;express&quot;);
  const passport = require(&quot;passport&quot;);
  const session = require(&quot;express-session&quot;);
  const cors = require(&quot;cors&quot;);
  const LocalStrategy = require(&quot;passport-local&quot;).Strategy;
  const MongoStore = require(&quot;connect-mongo&quot;);
  
  const mongoose = require(&quot;mongoose&quot;);
  
  const connection = mongoose.createConnection(
    &quot;mongodb://localhost:27017/login&quot;,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    },
    (err) =&gt; {
      if (err) console.log(err);
    }
  );
  
  const UserSchema = new mongoose.Schema({
    username: String,
    password: String,
  });
  
  const User = connection.model(&quot;User&quot;, UserSchema);
  
  const app = express();
  
  app.use(cors({ credentials: true, origin: &quot;http://localhost:3000&quot; }));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  
  passport.use(
    new LocalStrategy((username, password, done) =&gt; {
      User.findOne({ username: username })
        .then((user) =&gt; {
          if (!user) {
            console.log(&quot;no user found&quot;);
            return done(null, false);
          }
  
          const isValidPassword = user.password === password;
          if (isValidPassword) {
            return done(null, user);
          } else {
            return done(null, false);
          }
        })
        .catch((err) =&gt; {
          return done(err);
        });
    })
  );
  
  passport.serializeUser((user, done) =&gt; {
    console.log(user._id);
    done(null, user._id);
  });
  
  passport.deserializeUser((id, done) =&gt; {
    User.findById(id).then((error, user) =&gt; {
      if (error) {
        return done(error);
      }
  
      return done(null, user);
    });
  });
  
  // const sessionStore = new MongoStore({
  //   mongooseConnection: connection,
  //   collection: &quot;sessions&quot;,
  // });
  
  const sessionStore = MongoStore.create({
    mongoUrl: &quot;mongodb://localhost:27017/login&quot;,
  });
  
  app.use(
    session({
      secret: &quot;some secret&quot;, // long random string in prod
      resave: false,
      saveUninitialized: false,
      store: sessionStore,
      cookie: {
        maxAge: 1000 * 60, // 60 minutes
        secure: false,
      },
    })
  );
  
  app.use(passport.initialize());
  app.use(passport.session());
  
  app.post(
    &quot;/login&quot;,
    passport.authenticate(&quot;local&quot;, {
      successRedirect: &quot;/login-success&quot;,
      failureRedirect: &quot;/login-failure&quot;,
    }),
    (err, req, res, next) =&gt; {
      if (err) {
        next(err);
      }
    }
  );
  
  app.get(&quot;/login-success&quot;, (req, res) =&gt; {
    res.json({ message: &quot;all good&quot; });
  
    console.log(&quot;logged in with session: \n&quot;, req.session, &quot;\n&quot;);
  });
  
  app.get(&quot;/login-failure&quot;, (req, res) =&gt; {
    console.log(&quot;failed to log in&quot;);
    res.json({ message: &quot;nope&quot; });
  });
  app.get(&quot;/dashboardData&quot;, (req, res) =&gt; {
    console.log(req.session, req.isAuthenticated());
    if (req.isAuthenticated()) {
      res.sendStatus(200);
    } else {
      res.sendStatus(401);
    }
  });
  const port = 5000;
  app.listen(port);
  </code></pre>