简体   繁体   English

node.js 中基于角色的登录身份验证

[英]Role based login authentication in node.js

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();
}
});

You can do this at the SQL layer, this is an easy way of doing this.您可以在 SQL 层执行此操作,这是执行此操作的简单方法。 I'm assuming there is an fyp_employers table.我假设有一个 fyp_employers 表。

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();
    }
});

I would create 2 private functions one for checking its an employe and one for checking if its an helper.我将创建 2 个私有函数,一个用于检查其员工,另一个用于检查其是否为帮手。

This would make it easyer in the future to make changes like 2 seperate login forms or diffrent functionalities behind it.这将使将来更容易进行更改,例如 2 个单独的登录表单或背后的不同功能。

I also would make a role based model but for this i suggest you folow a guide .我也会制作一个基于角色的模型,但为此我建议您遵循指南

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM