Role based login authentication in node.js

palo :

i have made two forms to register employers and helpers. Now i want to do login authentication with only one form. How can i do this such that when user clicks on login the values match with both employers and helpers table and whereever the values match it just login? This is for one.

app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.pwd;
if (username && password) {
    connection.query('SELECT * FROM fyp_helpers 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');
        } else {
            response.send('Incorrect Username and/or Password!');
        }           
        response.end();
    });
} else {
    response.send('Please enter Username and Password!');
    response.end();
}
});
Terry Lennox :

You can do this at the SQL layer, this is an easy way of doing this. I'm assuming there is an fyp_employers table.

app.post('/auth', function(request, response) {
    var username = request.body.username;
    var password = request.body.pwd;
    if (username && password) {
        connection.query('SELECT * FROM fyp_helpers WHERE username = ? AND password = ? UNION SELECT * FROM fyp_employeers WHERE username = ? AND password = ?', [username, password, username, password], function(error, results, fields) {
            if (error) {
                console.error("An error occurred:", error);
                response.send('Oops, something went wrong!');
            } else if (results.length > 0) {
                request.session.loggedin = true;
                request.session.username = username;
                response.redirect('/home');
            } else {
                 response.send('Incorrect Username and/or Password!');
            }           
            response.end();
        });
    } else {
        response.send('Please enter Username and Password!');
        response.end();
    }
});

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170084&siteId=1