Home > Blockchain >  Validation of username OR phone number - compare
Validation of username OR phone number - compare

Time:03-03

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");
    }
});

CodePudding user response:

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: "[email protected]",
    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('[email protected]', '0490')); //email
console.log(checkUserExists('[email protected]', '079000000')); //phone
console.log(checkUserExists('[email protected]', '079000000')); //both
console.log(checkUserExists('[email protected]', '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");
    }
});
  • Related