Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
How can I display a specific error based on what's wrong?

I would like to display a specific error instead of the generic "wrong credentials"

For example, if someone inserts the password but the email is right, only "password is wrong" should appear.

How can I achieve that?

app.post('/login', async (req, res) => {
  const client = new MongoClient(url)
  const data = req.body.formData

  try {
    await client.connect()
    const database = client.db("data")
    const collection = database.collection("utenti")

    const result = await collection.findOne({
      email: data.email,
      password: data.password
    })
    console.log(result)

    if (result.email && result.password) {
      res.status(200).send("Success")
    }
  } catch (err) {
    res.status(400).send("Wrong credentials")
  }
})
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

To achive what you want, need first check the database to find the user with his email.

Then, if you find it you can check if password is ok, if you don't find the user you be able to send the information back.

Then if password is ok you can loged user in, if not send a message about the wrong password.

But once again, please, encrypted your password! And less information you give to an attacker, the best secured is your application.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I recommend not displaying that "password is incorrect" because it will create a loophole in your authentication. But I have provided a code to check the password separately from its message.

app.post('/login', async (req, res) => {
const client = new MongoClient(url)
const data = req.body.formData

  try {
    await client.connect()
    const database = client.db("data")
    const collection = database.collection("utenti")

    const email = await collection.findOne({
      email: data.email,
    })
    
    console.log("email is correct", email);

    if (!email) {
     res.status(400).send("Your email is incorrect")
     }

    const password = await collection.findOne({
      password: data.password,
    });

    if (email && password) {
      res.status(200).send("Success")
    }

    if (!password) {
     res.status(400).send("Only Email is correct and password is incorrect", email);
    }
  } catch (err) {
    res.status(400).send("Wrong credentials")
  }
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

you should validate individually in some function(in your case for email and password) and throw specific exceptions.

Something like this (in python):

fun validate_email_password(email, password):
        email = db.findByEmail(email)
        if not email:
            throw EmailNotFound()
        password = db.findByPassword(password)
        if not password:
            throw passwordNotFound()

catch these specific exceptions in your code and throw them. This solution has db cost.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda