Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!