Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda