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

275
Vistas
Validation of username OR phone number - compare

I am currently just doing a very simple and basic login system with either a email address or a phone number. No password.

Nothing major to hide, just an information hub.

What i want to achieve:

  • Check if user entered email exists within users array.
  • Check if users entered phone exists within users array. Check if both email and phone match the same user.
  • Check if email is wrong and phone is drop (or vice versa) - it still finds the right user.

Whats currently happening:

  • It finds the right user if the email is entered in.
  • If email is entered in wrong and phone number is correct, i get a undefined error.
  • If both are wrong i get a server error stating email is undefined.

The code:

Users object

const users = [
    {
        email: "jon@doe.com",
        phone: "0798888888",
        name: "John Doe",
        access: ["home", "blah", "etc"]
    },
    {
        email: "fakedoe@john.com",
        phone: "079000000",
        name: "Fake Doe",
        access: ["home", "etc"]
    }
];

Main code:

app.post("/in", async (req, res) => {
    let email = req.body.email,
        phone = req.body.phone;

    let conditions = !!email ? { email } : { phone };
    let data = users.find((x) => x.email === conditions.email || x.phone === conditions.phone);
    let pass = data.email || data.phone !== undefined;

    console.log(pass);

    if (pass) {
        if (conditions.email && conditions.email === data.email) {
            pass = true;
        }

        if (conditions.phone && conditions.phone === data.phone) {
            pass = true;
        }
    }

    if (pass) {
        res.cookie("JWT", jwtSign(req.body.email, data.name, data.access));
        res.status(200);
        res.send("OK");
    } else {
        res.status(200);
        res.send("Invalid user/password");
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

login system with either a email address or a phone number

Had to find a way to play around with the code. Here is a working version!

const users = [{
    email: "fakedoe@john.com",
    phone: "079000000",
    name: "Fake Doe",
    access: ["home", "etc"]
  }
];
function checkUserExists(email, phone) {
  return !!users.find((x) => x.email === email || x.phone === phone);
}

console.log(checkUserExists('fakedoe@john.com', '0490')); //email
console.log(checkUserExists('fakedoe@joh.com', '079000000')); //phone
console.log(checkUserExists('fakedoe@john.com', '079000000')); //both
console.log(checkUserExists('fakedoe@joh.com', '0490')); //none

So replace this:

app.post("/in", async (req, res) => {
    let email = req.body.email,
        phone = req.body.phone;

    let conditions = !!email ? { email } : { phone };
    let data = users.find((x) => x.email === conditions.email || x.phone === conditions.phone);
    let pass = data.email || data.phone !== undefined;

    console.log(pass);

    if (pass) {
        if (conditions.email && conditions.email === data.email) {
            pass = true;
        }

        if (conditions.phone && conditions.phone === data.phone) {
            pass = true;
        }
    }

    if (pass) {
        res.cookie("JWT", jwtSign(req.body.email, data.name, data.access));
        res.status(200);
        res.send("OK");
    } else {
        res.status(200);
        res.send("Invalid user/password");
    }
});

with this:

function userExists(email, phone) {
  return !!users.find((x) => x.email === email || x.phone === phone);
}
app.post("/in", async (req, res) => {
    if (userExists(req.body.email, req.body.phone)) {
        res.cookie("JWT", jwtSign(req.body.email, data.name, data.access));
        res.status(200);
        res.send("OK");
    } else {
        res.status(200);
        res.send("Invalid user/password");
    }
});
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