javascript - Making a login page using JSON as verification - Stack Overflow

First question on Stackoverflow, still new to programming.I'm trying to make a simple bank login

First question on Stackoverflow, still new to programming.

I'm trying to make a simple bank login page with a JSON file that has all usernames and passwords in it.

I'm making an if statement that will read the JSON file to find the username and password as a key and value to match the user's input on the website. If it matches the input, it will login to the website, otherwise, it will either say "Invalid Password" or "Not a registered Username".

What's happening is that it keeps giving me "Not a registered email" when I typed the correct username and password but I want to login with the correct username and password.

I'm using node.js and express (not sure if it matters?). Sorry if my code or explanation isn't clear, still very new.

...
var accounts = JSON.parse(fs.readFileSync('./accounts.json'));

app.post('/', function (req, res) {
    username1 = req.body.userinput; //username is their email
    password1 = req.body.passinput; 

    
    if (username1 == accounts.email && password1 == accounts.password) {
        console.log("Correct login");
        res.render('login', { layout: false});
    }

    else if (username1 == accounts.email && password1 != accounts.password) {

        var incorrectp = ("Invalid Password");
        res.render('login', { layout: false, wrongp: incorrectp });
    }

    else if (username1 != accounts.email) {

        var incorrectu = ("Not a registered username");
        res.render('login', { layout: false, wrongu: incorrectu });
    }

});

accounts.json

[
    {
    "id": "100001",
    "email":"[email protected]",
    "password":"helloworld",
    "accountType":"Chequing",
    "accountBalance":0
 },
 {
    "id": "100002",
    "email":"[email protected]",
    "password":"lennon",
    "accountType":"Savings",
    "accountBalance":0
 }
]
...

First question on Stackoverflow, still new to programming.

I'm trying to make a simple bank login page with a JSON file that has all usernames and passwords in it.

I'm making an if statement that will read the JSON file to find the username and password as a key and value to match the user's input on the website. If it matches the input, it will login to the website, otherwise, it will either say "Invalid Password" or "Not a registered Username".

What's happening is that it keeps giving me "Not a registered email" when I typed the correct username and password but I want to login with the correct username and password.

I'm using node.js and express (not sure if it matters?). Sorry if my code or explanation isn't clear, still very new.

...
var accounts = JSON.parse(fs.readFileSync('./accounts.json'));

app.post('/', function (req, res) {
    username1 = req.body.userinput; //username is their email
    password1 = req.body.passinput; 

    
    if (username1 == accounts.email && password1 == accounts.password) {
        console.log("Correct login");
        res.render('login', { layout: false});
    }

    else if (username1 == accounts.email && password1 != accounts.password) {

        var incorrectp = ("Invalid Password");
        res.render('login', { layout: false, wrongp: incorrectp });
    }

    else if (username1 != accounts.email) {

        var incorrectu = ("Not a registered username");
        res.render('login', { layout: false, wrongu: incorrectu });
    }

});

accounts.json

[
    {
    "id": "100001",
    "email":"[email protected]",
    "password":"helloworld",
    "accountType":"Chequing",
    "accountBalance":0
 },
 {
    "id": "100002",
    "email":"[email protected]",
    "password":"lennon",
    "accountType":"Savings",
    "accountBalance":0
 }
]
...
Share Improve this question edited Jan 25, 2022 at 19:55 frozenleafz asked Jan 25, 2022 at 19:51 frozenleafzfrozenleafz 391 gold badge2 silver badges5 bronze badges 5
  • Looks like when you're trying to call username1 == accounts.email, accounts has been read as an array so accounts.key will always be undefined, perhaps you should try looping over the accounts array and access it that way – Patrick Barr Commented Jan 25, 2022 at 19:55
  • So I make a for loop like this? if (username1 == accounts[i].email && password1 == accounts[i].password) ? – frozenleafz Commented Jan 25, 2022 at 19:59
  • 1 "password":"helloworld", - please don't do this. Never do this. – evolutionxbox Commented Jan 25, 2022 at 20:00
  • @frozenleafz yep! – Patrick Barr Commented Jan 25, 2022 at 20:02
  • thank you everyone! I will try everyone's suggestions :) – frozenleafz Commented Jan 25, 2022 at 20:02
Add a ment  | 

2 Answers 2

Reset to default 0

You would want to iterate through accounts.json.

Try something like this.

let acc;

for (const account of accounts) {
    if (username1 == account.email) {
        acc = account;
    }
}

if (acc == null) {
    var incorrectu = ("Not a registered username");
    res.render('login', { layout: false, wrongu: incorrectu });
    return;
}

// Do your account checking logic here

This approach won't scale very good. You can do this using forEach:

let accountName;
accounts.forEach(obj => { 
  if (obj.email === username1 && obj.password === password1) {
    accountName = obj.email
  })

or by using find:

const accountName = accounts.find(obj => (obj.email === username1 && obj.password === password1))

Besides the actual question, I want to emphasize that you should never store password in clear or encrypted form. A password hashing mechanism must be used, possible candidates include: Argon2, bcrypt or PBKDF2. Make sure you use appropriate parameters for these mechanisms.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745088308a4610538.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信