Take Json value with Node

Asked

Viewed 350 times

0

I want to login to a site, using Node. The login would perform a select in the database, according to the E-mail entered, and then check if the password is in agreement. In login Submit, I perform select in the database, and user data is displayed on the page in JSON format.

app.post('/login', function(req, res){
var email = req.body.email;
var senha = req.body.senha;
senha = criptografar(senha);

execSQL("select * from Usuario where email = '"+ email+"' ", res);

if(senha == *senha recuperada do json*)
     logar();
});


function execSQL(sql, resposta) {
global.conexao.request()
.query(sql)
.then(resultado => resposta.json(resultado.recordset))
.catch(erro => resposta.json(erro));
}

The json displayed on the localhost:3000 page is :

[{
  "codUsuario":100,
  "nome":"Pedro Pereira",
  "cpf":"042.321.123-22",
  "email":"[email protected]",
  "telefone":"(19)1212-3232",
  "senha":"112233",
  "peso":50,
  "altura":1,
  "codNutricionista":4999,
  "Pontuação":0
}]

How I got the password displayed on the page?

  • You need to send the password through the request, nay?

1 answer

1


You need to wait for the query return data and do the password check. You can do this using async/await. Here is an example:

app.post('/login', async function(req, res){
	var email = req.body.email;
	var senha = req.body.senha;
	senha = criptografar(senha);
	
	var sqlQry1 = "select * from Usuario where email = '"+ email+"' ";
	let resultados = await global.conn.request().query(sqlQry1);
	resultados.recordset.forEach(function(item) {
		if(senha == item.senha){
			logar();
		}	 
	});	
});

For the example of your return, the password is not encrypted. In your code, you receive the posted password and perform a function called encrypt. If the password is saved without encryption, the comparison will not work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.