Dognation passport.authenticate not attaching a req.user

I am currently trying to complete the Dognation project which uses node and express to add a login feature that properly hashes and stores users.

The problem im running into is according to the codeacademy task I should be able to access a req.user object after I have authenticated a user with passport.authenticate but I am console logging it and its always undefined.

passport.js

const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
const helper = require("../helpers/helper");

// Set up the Passport strategy:
passport.use(new LocalStrategy(function(username, password, done) {
  helper.findByUsername(username, async function(err, user) {
    if(err) return done(err);
    if(!user) return done(null, false);
    const match = await bcrypt.compare(password, user.password);
    if(!match) return done(null, false);
    console.log('passed!');
    return done(null, user);
  });
}));
// Serialize a user
passport.serializeUser(function(user, done) {
  done(null, user.id);
});
// Deserialize a user
passport.deserializeUser(function(id, done) {
  helper.findById(id, function(err, user) {
    if (err) return done(err);
    return done(null, user);
  });
});

users.routes.js

const express = require("express");
const router = express.Router();
const helper = require("../helpers/helper");
const passport = require("passport");
const filename = "./data/users.json";
const bcrypt = require("bcrypt");
let users = require("../data/users.json");

// Register New User:
router.post("/register", async (req, res) => {
  const { username, password } = req.body;
  const id = { id: helper.getNewId(users) };
  try {
    const user = await helper.userExists(username);
    if (user) {
      console.log("User already exists!");
      return res.redirect("login");
    }
    // Hash password before storing in local DB:
  const salt = await bcrypt.genSalt(10);
  const hashedPassword = await bcrypt.hash(password, salt);

    const newUser = { ...id, username, password: hashedPassword };

    // Store new user in local DB
    await users.push(newUser);
    await helper.writeJSONFile(filename, users);

    res.redirect("login");
  } catch (err) {
    res.status(500).json({ message: err.message });
  }
});

// Log In User:
router.post("/login", passport.authenticate('local', {failureRedirect: '/login'}),
(req, res) => {
  res.redirect('/');
}
);

// Log out user:
router.get("/logout", (req, res) => {
  req.logout();
  res.redirect("../");
});

router.get("/register", (req, res) => {
  res.render("register");
});

router.get("/login", (req, res) => {
  res.render("login");
});

module.exports = router;
// Import packages
const express = require("express");
const app = express();
const session = require("express-session");
const passport = require("passport");

// App config
app.set("trust proxy", 1);
const PORT = process.env.PORT || 4001;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.set("view engine", "ejs");
// Import Passport config
require("./config/passport");

// Session Config
app.use(
  session({
    secret: 'secret',
    cookie: { maxAge: 170222, secure: false },
    saveUninitialized: false,
    resave: false,
    sameSite: 'none',
    secure: true
  })
);
// Passport Config
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use(require("./routes/index.routes"));

app.get("/", (req, res) => {
  console.log(req.user);
  const user = req.user || 'Guest';
  
  res.render("home", { user });
});


app.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`);
});

I’m having the same issues. I can create accounts and login but homepage still has me logged in a “Guest” and the session doesn’t seem to be holding the user. My code looks pretty much identical to yours.