How to use connect-flash with passport-local

hello every one i was trying to use connect-flash with passport local but can not seem to find relevant info either on the docs and other sources,if any one can guide me on this and tell me what i went wrong ,thanks.And why does request to login-fail gives this error:-
ERROR is AxiosError: Request failed with status code 404
but when i test the route with rest client on vscode it responses perfectly like seen below.


also when i check the database right after i click submit on the route there will be two sessions stored consecutively one having the flash(the first) and the other not like this:-

session:
{"cookie":{"originalMaxAge":259200000,"expires":"2023-01-16T12:30:14.395Z","secure":false,"httpOnly":true,"path":"/"},"flash":{"error":["Incorrect password"]}}


session:
{"cookie":{"originalMaxAge":259200000,"expires":"2023-01-16T12:30:14.453Z","secure":false,"httpOnly":true,"path":"/"}}

why this is happening?
userRoutes.js

const router = require("express").Router();
const passport = require("passport");
const { ObjectID } = require("mongodb");
const LocalStrategy = require("passport-local");
const bcrypt = require("bcrypt");
const flash = require("connect-flash");

const User = require("../models/userModel.js");

router.route("/register").post(
  (req, res, next) => {
    User.findOne({ fullName: req.body.fullName }, (err, user) => {
      if (err) {
        next(err);
      } else if (user) {
        res.redirect("/");
      } else {
        const {
          fullName,
          email,
          id,
          department,
          stream,
          batch,
          sex,
          age,
          phoneNumber,
          password,
        } = req.body;
        const hash = bcrypt.hashSync(password, 12);
        console.log(hash);
        const newUser = new User({
          fullName,
          email,
          id,
          department,
          stream,
          batch,
          sex,
          age,
          phoneNumber,
          password: hash,
        });
        newUser.save((err, data) => {
          if (err) res.redirect("/");
          next(null, data);
        });
      }
    });
  },
  passport.authenticate("local", { failureRedirect: "/" }),
  (req, res, next) => {
    res.json({ user: "i am josh" });
  }
);

router
  .route("/login")
  .post(
    passport.authenticate("local", {
      failureRedirect: "/login-fail",
      failureFlash: true,
    }),
    (req, res) => {
      res.json({ user: "i am josh" });
    }
  );

router.route("/login-fail").get((req, res) => {
  // console.log(req.flash('error'));
  res.json({ user: "josh" });
});

router.route("/logout").get((req, res) => {
  req.logout();
  res.redirect("/");
});

passport.serializeUser((user, done) => {
  done(null, user._id);
});

passport.deserializeUser((id, done) => {
  User.findOne({ _id: new ObjectID(id) }, (err, doc) => {
    done(null, doc);
  });
});

const customFields = {
  usernameField: "email",
};

passport.use(
  new LocalStrategy(customFields, (email, password, done) => {
    User.findOne({ email }, (err, user) => {
      //console.log(`User ${user.username} attempted to log in.`);
      if (err) return done(err);
      if (!user) return done(null, false, { message: "email does not exist" });
      if (!bcrypt.compareSync(password, user.password)) {
        return done(null, false, { message: "Incorrect password" });
      }
      return done(null, user);
    });
  })
);

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  res.json({ msg: "You are not authorized to view this page" });
}

module.exports = router;

server.js

require("dotenv").config();
const express = require("express");
const app = express();
const flash = require("connect-flash");
const session = require("express-session");
const passport = require("passport");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const cors = require("cors");

app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
app.use(cors());

app.use(flash());
app.use(
  session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: true,
    store: MongoStore.create({
      mongoUrl: process.env.DB,
      collection: "sessions",
    }),
    cookie: { secure: false, maxAge: 1000 * 60 * 60 * 24 * 3 },
  })
);

app.use(passport.initialize());
app.use(passport.session());

mongoose
  .connect(process.env.DB)
  .then(() => {
    console.log("DB connection successful");
  })
  .catch((err) => {
    console.log(`DB connection Error: ${err}`);
  });

const usersRouter = require("./routes/userRoutes");

app.use("/user", usersRouter);

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`server is running on port ${PORT}`);
});

module.exports = app;

this error seems to occur due to this.
about connect-flash ,well i left using it.instead used another simple method to flash the messages,not awesome but good enough.but any one having better ideas do not forget to mention,thanks for all.