'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: "[email protected]",
        phone: "0798888888",
        name: "John Doe",
        access: ["home", "blah", "etc"]
    },
    {
        email: "[email protected]",
        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");
    }
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source