0

I'm a newbie at node.js, and trying to make a login page.

When I'm trying to login with the right credentials the page works as intended. Also worth noting is that when I try to login with no credentials the alert window pops up. But when I try to login with the wrong credentials, it just won't work. I've tracked the error back to "alert" function, but I cannot understand why in the world the alert function works when calling it to display the alert that no username nor password was inserted, but will crash when I'm trying to display that the username and password are wrong. I'm pretty certain that the error comes from the alert function.

The code is below.

Thanks alot!

app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
    connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
        if (results.length > 0) {
            request.session.loggedin = true;
            request.session.username = username;
            response.redirect('/home');
            response.end();
        } else {
            alert('Incorrect Username and/or Password!');
        }
    });
} else {
    alert('Please enter Username and Password!');
}
});
2
  • Danger: "Not hashing at all" is an unsuitable hashing algorithm; you need to take better care of your users' passwords. Commented Jan 19, 2022 at 10:53
  • 1
    It's a proof of concept... I'm trying to learn for the moment Commented Jan 19, 2022 at 11:04

1 Answer 1

1

but I cannot understand why in the world the alert function works when calling it to display the alert that no username nor password was inserted

It shouldn't do.

alert isn't a function provided by Node.js. It belongs to browsers so it can show something in the browser UI.

To send something from a server built on Node.js back to the browser then:

  • The something needs to be data
  • You need to send it by calling a method (like render, json or send) on the response object.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.