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

135
Views
Making a login page using JSON as verification

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":"helloworld@gmail.com",
    "password":"helloworld",
    "accountType":"Chequing",
    "accountBalance":0
 },
 {
    "id": "100002",
    "email":"john@beatles.uk",
    "password":"lennon",
    "accountType":"Savings",
    "accountBalance":0
 }
]
...
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

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

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!