Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
nodejs mongoose - how to check items in the database before login

In my project, I've different roles (seller/user/admin)and i want to check the role and redirect to specific page if they are seller for example.

I struggle on how i can check the role in Mongo DB before the login. My login page is basic email-password and submit button.

for my signup all is good, it's use the correct model and post it in the DB.

here are some pieces of my code:

(client model)

userSchema.statics.login = async function (email, password, role) {
  const user = await this.findOne({ email });

  if (user) {
    const auth = await bcrypt.compare(password, user.password);

    if (auth) {
      return user;
    }
    throw Error("incorrect password");
  }

  throw Error("incorrect email");
};

const ClientModel = mongoose.model("client", userSchema, "users");

login controller:

module.exports.clientSignIn = async (req, res) => {
  const { email, password } = req.body;
  
  try {
    const user = await LoginModel.login(email, password);
    const token = createToken(user._id);
    res.cookie("jwt", token, { httpOnly: true, maxAge });
    res.redirect('/success');
  } catch (err) {
    console.log(err.message);
  }
};

thanks in advance for your help, if you need more info please feel free to ask

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Following @EAzevedo 's advice.

i just change my Controller

module.exports.clientSignIn = async (req, res) => {
  const { email, password } = req.body;

  try {
    const user = await LoginModel.login(email, password);
    const token = createToken(user._id);
    res.cookie("jwt", token, { httpOnly: true, maxAge });
    if (user.role == "client") {
      res.redirect("/success");
    } else if (user.role == "technicien") {
      res.redirect("/success-technicien");
    } else if (user.role == "superuser") {
      res.redirect("/success-admin");
    };
  } catch (err) {
    const errors = signInErrors(err);
    res.status(200).json({ errors });
  }
};
about 4 years ago · Juan Pablo Isaza Relatório

0

when you get the user , you should have field for the role , then check which role logged in and redirect him to where he needs to be

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda