79627771

Date: 2025-05-18 19:44:56
Score: 1.5
Natty:
Report link

To use two different authentication you need to add one more thing in your mongoose schema that is usertype like that

const mongoose = require("mongoose");
const passportLocalMongoose = require("passport-local-mongoose");

const sellerSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    minlength: [6, "Username is Too Short"],
    maxlength: [25, "Username is Too long"],
  },
  email: {
    type: String,
    required: true,
    minlength: [10, "Email is Too Short"],
    maxlength: [100, "Email is Too long"],
  },
  userType: { 
    type: String, 
    default: "seller"
  },
});

sellerSchema.plugin(passportLocalMongoose);

const seller_account = new mongoose.model("seller", sellerSchema);
module.exports = seller_account;

you can make more than one schema

after that just require the schema into app.js

const User = require("./model/user_account");
const Seller = require("./model/seller_account");

and then initialize the passport.js and authenticate both schema as per your usage here i assign two local strategies 'user' and 'seller'

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

passport.use("user", new LocalStrategy(User.authenticate()));
passport.use("seller", new LocalStrategy(Seller.authenticate()));

lets see how i use these two strategies in different routes

The first one gets a /login request and render a login page

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

The second one gets a /seller/login request and render a seller login page

router.get("/seller/login", async (req, res) => {
  res.render("./pages/seller_login.ejs");
});

after the i use two strategies like that here passport.authenticate("seller" or "user"........

router.post(
  "/login_seller",
  saveReturnTo,
  passport.authenticate("seller", {
    failureRedirect: "/seller/login",
    failureFlash: true,
  }),
  async (req, res) => {
    req.flash("success", "Login Account Successfully");
    let redirectUrl =  res.locals.returnTo || "/";
    res.redirect(redirectUrl);
  }
);

router.post(
  "/login_user",
  saveReturnTo,
  passport.authenticate("user", {
    failureRedirect: "/login",
    failureFlash: true,
  }),
  async (req, res) => {
    req.flash("success", "Login Account Successfully");
    let redirectUrl =  res.locals.returnTo || "/";
    res.redirect(redirectUrl);
  }
);

but there is a fault i.e when i login as a user or as a seller i stil get access to both user and seller pages LOL so dont worry for this bug i use a middleware which is placed in different file and export it anywhere that there i need to authenticate that the person is login as a user or as a seller the middileware is like that

module.exports.is_Seller = (req, res, next) => {
 if(req.user.userType === "seller"){
    return next();
 }  
  req.flash("error", "please login as seller");
  res.redirect("/seller/login");
}

you can see how i use middleware

router.get("/seller/home", isLoggedIn, is_Seller, async (req, res) => {
  let data = await product.find({});
  res.render("./pages/seller/home.ejs", { data });
});

here you can see first i check that the person is login or not

module.exports.isLoggedIn = (req, res, next) => {
  if (!req.isAuthenticated()) {
    req.session.returnTo = req.originalUrl;
    req.flash("error", "You need to be logged in to do that");
    return res.redirect("/login");
  }
  next();
}

then i check that the login person is seller or not by is_seller middleware also i use connect-flash npm package to flash a error or success message

if you like my answer please like my answer thankyou

Reasons:
  • Blacklisted phrase (1): thankyou
  • Blacklisted phrase (0.5): i need
  • Long answer (-1):
  • Has code block (-0.5):
  • Filler text (0.5): ........
  • Low reputation (1):
Posted by: Mohammad Maaz