Failed to serialize user into session while logging in

The user registration part was successful, but while logging in, i get Failed to serialize the user into a session even though I have defined serializeUser and deserializeUser

The serialize and deserialize was used from passport.js middleware for node.js.

Packages used (all from npm) : passport, passport-local, passport-local-mongoose, express-session

error log : Successfully running on port 3000 Error: Failed to serialize user into session

    const app = express();
    
    app.use(express.static("public"));
    app.set('view engine','ejs');
    app.use(bodyParser.urlencoded({extended:true}));
    
    secrets = process.env.SECRETS;
    
    //using the session
    app.use(session({
      secret:secrets,
      resave:false,
      saveUninitialized:false
    }));
    
    //initializing and using passport
    app.use(passport.initialize());
    app.use(passport.session());
    
    
    //connect the db
    mongoose.connect("mongodb://localhost:27017/userDB",{useNewUrlParser:true, useUnifiedTopology: true});
    //create schema
    const userSchema = new mongoose.Schema({
      email : String,
      password : String
    });
    //avoid warning, USE: useCreateIndex
    mongoose.set("useCreateIndex",true);
    
    
    //adding passportLocal plugin
    userSchema.plugin(passportLocalMongoose);
    
    
    
    //mongoose-encryption
    userSchema.plugin(encrypt,{secret:secrets,encryptedFields:["password"]});
    
    //create model
    const User = mongoose.model("User",userSchema);
    
    //passportLocal config
    passport.use(User.createStrategy());
    
    passport.serializeUser(User.serializeUser());
    passport.deserializeUser(User.deserializeUser());
    
    
    
    ///////GET///////////
    app.get("/",function(req,res){
      res.render("home");
    });
    
    app.get("/register",function(req,res){
      res.render("register");
    });
    
    app.get("/login",function(req,res){
      res.render("login");
    });
    
    app.get("/secrets",function(req,res){
      if(req.isAuthenticated()){
        res.render("secrets");
      } else {
        res.redirect("/login");
      }
    });
    
    app.get("/logout",function(req,res){
      req.logout();
      res.redirect("/");
    });
    
    
    ///////POST///////////
    app.post("/register",function(req,res){
    
      User.register({username:req.body.username},req.body.password,function(err,user){
        if(err){
          console.log(err);
          res.redirect("/register");
        } else {
          passport.authenticate("local")(req,res,function(){
            res.redirect("/secrets");
          });
        }
      });
    
    });
    
    app.post("/login",function(req,res){
      const user = new User({
        email : req.body.username,
        password : req.body.password
      });
    
      req.login(user,function(err){
        if(err){
          console.log(err);
    
        } else {
          passport.authenticate("local")(req,res,function(){
            res.redirect("/secrets");
          });
        }
      });
    });
    
    
    app.listen(3000,function(){
      console.log("Successfully running on port 3000");
    })

Answer

You need to serializeUser/deserializeUser

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

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