带节点的简单用户登录验证模块 [英] simple user login validation module with node

查看:58
本文介绍了带节点的简单用户登录验证模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个(非教程)节点应用程序,并且正在编写一个函数,该函数应该将用户名和密码作为参数,并针对我的数据库的用户表查询它们以返回 true或假.数据库已设置,应用程序已成功连接到数据库.

I'm writing my first (non tutorial) node application and am at a point where I'm writing a function that should take the username and password as parameters and query them against the user table of my database to return either true or false. The database is setup, and the app is connecting to it successfully.

但是,我没有经常使用 SQL,也没有使用 node,我不确定如何继续使用这个函数(以及简短的周围脚本).这是:

However, I haven't worked with SQL very much, nor node, and I'm unsure how to proceed with this function (and short surrounding script). Here it is:

console.log('validator module initialized');
var login = require("./db_connect");

function validate(username, password){

connection.connect();
console.log('Connection with the officeball MySQL database openned...');

connection.query(' //SQL query ', function(err, rows, fields) {
    //code to execute

});

connection.end();
console.log('...Connection with the officeball MySQL database closed.');


    if(){ //not exactly sure how this should be set up
        return true;
    }
    else{ //not exactly sure how this should be set up
        return false;
    }
}

exports.validate = validate;

这是使用 node-mysql.我正在寻找有关如何设置查询和验证的基本示例.

This is using node-mysql. I'm looking for a basic example of how I might set the query and validation up.

推荐答案

我认为您需要将您的应用程序重新考虑为一种更类似于节点的方式(即识别许多/大多数事情异步发生的方式,因此您'通常不会从这样的函数返回",而是从中进行回调.不确定您打算从 node-mysql 获取什么,但我可能只使用普通的 mysql 模块.以下代码仍然很可能不是完全是你想要的,但希望能让你正确地思考它.

I think you'll want to rethink your app into a more node-like way (i.e. one that recognizes that many/most things happen asynchronously, so you're not usually "returning" from a function like this, but doing a callback from it. Not sure what you plan to get from node-mysql, but I would probably just use the plain mysql module. The following code is still most likely not entirely what you want, but will hopefully get you thinking about it correctly.

请注意,下面使用的return"实际上并没有返回结果(回调本身不应该返回任何内容,因此它就像返回 undefined.返回语句在那里,因此您可以退出函数,这节省了很多乏味的 if/else 块.

Note that the use of 'return' below is not actually returning a result (the callback itself should not return anything, and thus its like returning undefined. The return statements are there so you exit the function, which saves a lot of tedious if/else blocks.

希望这会有所帮助,但我建议您查看 github 上的各种节点项目,以更好地了解节点编写的异步性质.

Hope this helps, but I'd suggest looking at various node projects on github to get a better feel for the asynchronous nature of writing for node.

function validate(username, password, callback){
    var connection = mysql.createConnection({ user:'foo',
                            password: 'bar',
                            database: 'test',
                            host:'127.0.0.1'});

    connection.connect(function (err){
        if (err) return callback(new Error('Failed to connect'), null);
        // if no error, you can do things now.

        connection.query('select username,password from usertable where username=?',
                username,
                function(err,rows,fields) {
                    //  we are done with the connection at this point), so can close it
                    connection.end();

                    // here is where you process results
                    if (err)
                        return callback(new Error ('Error while performing query'), null);
                    if (rows.length !== 1)
                        return callback(new Error ('Failed to find exactly one user'), null);

                    // test the password you provided against the one in the DB.
                    // note this is terrible practice - you should not store in the
                    // passwords in the clear, obviously. You should store a hash,
                    // but this is trying to get you on the right general path

                    if (rows[0].password === password) {
                        // you would probably want a more useful callback result than 
                        // just returning the username, but again - an example
                        return callback(null, rows[0].username);
                    } else {
                        return callback(new Error ('Bad Password'), null);
                    }

                });


    });
};

这篇关于带节点的简单用户登录验证模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆