How to authenticate multiple Collections using passport NodeJS

Viewed 10

I am making a project where i have manufacture login and Purchaser login i want to authenticate both of them using passport

 username:String,
 password:String
 });
 const purchaserSchema = new Schema({
  username:String,
  password:String
  }); 
passport.use(Manufacture.createStrategy());
passport.use(Purchaser.createStrategy());
let mode=0;
passport.deserializeUser(function(id, done) {
  console.log(mode);
  if(mode==1){
  Manufacture.findById(id, function(err, user) {
    done(err, user);
  });
}else{
  Product.findById(id, function(err, user) {
    done(err, user);
  });
}
});
app.post("/manufactureRegister",function(req,res){
  Manufacture.register({username:req.body.username},req.body.password,function(err,user){
    
    if(err)
    {
      console.log(err);
      res.redirect("/register");
    }
    else
    {
      mode=0;
      passport.authenticate("local")(req,res,function(err)
    {
      res.redirect("/secrets");
    });
    }
  });
});
app.post("/purchaserRegister",function(req,res){
  console.log("An errure");
  Purchaser.register({username:req.body.username},req.body.password,function(err,user){
    console.log("An error");
    if(err)
    { console.log("An error");
      console.log(err);
      res.redirect("/register");
    }
    else
    {
      mode=1;
      passport.authenticate("local")(req,res,function(err)
    {
      Product.find({}, function ( err,foundItems) {
        console.log(foundItems);
      });
      res.render("UserHome",{productData:foundItems});
    });
    }
  });
});

I have created two different schemas for manufacture and purchaser & want to authenticate them using passport library but when i authorise one other remain unauthorised.Kindly help me with code. passport.use(Manufacture.createStrategy()); passport.use(Purchaser.createStrategy()); this command makes only purchaser as strategy and handles only Purchaser authorisation as it is declared after manufacture strategy.

0 Answers
Related