这是针对数据库检查身份验证令牌。有人能发现这里有什么问题吗?
var checkauth = function(auth) {
var rs = 0;
var sql = 'SELECT * FROM clients WHERE pword=\''+auth+'\'';
// Copied from debug session and got results: SELECT * FROM clients WHERE pword='d98e623c7a74a178703d17e1fd536b1488724acd41e71f178331c768c385bda2c82d2bcb60cbb4650be375ad4734c63fb694bd164c138f9abe0c51f37f9a7e33'
var query = connection.query(sql);
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( row ) {
rs = Number(row.client_id); // This never fires!!?!?!
})
.on('end',function(){
});
if (rs == 0) {
sql = 'SELECT * FROM users WHERE pword=\''+auth+'\'';
query = connection.query(sql);
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( row ) {
rs = Number(row.client_id); // This never fires!!?!?!
})
.on('end',function(){
});
}
return rs;
}
编辑1:我使用follwoing循环来验证运行该函数的socket.io连接,好吧我知道我必须等待db完成,我将把回调放在哪里?
io.use(function(socket, next){
console.log("Query: ", socket.handshake.query);
// return the result of next() to accept the connection.
socket.clientid = 0;
socket.clientid = checkauth(socket.handshake.query.auth);
console.log("CID:"+socket.clientid);
if (socket.clientid != 0) {
return next();
}
// call next() with an Error if you need to reject the connection.
next(new Error('Authentication error'));
});
编辑2 /尝试1根据约翰内斯'建议:
var checkauth = function(auth, cb) {
var rs = 0;
var sql = 'SELECT * FROM clients WHERE pword=?';
var query = connection.query(sql, [auth]);
query
.on('error', function(err) {
console.log(err);
})
.on('result', function(row) {
rs = Number(row.client_id);
})
.on('end', function() {
if (rs == 0) {
sql = 'SELECT * FROM users WHERE pword=?';
query = connection.query(sql, [auth]);
query
.on('error', function(err) {
console.log(err);
})
.on('result', function(row) {
rs = Number(row.client_id);
})
.on('end', function() {
cb(rs);
});
}
});
}
io.use(function(socket, next){
console.log("Query: ", socket.handshake.query);
// return the result of next() to accept the connection.
socket.clientid = 0;
var auth = socket.handshake.query.auth;
checkauth(auth, function(clientid){
socket.clientid = clientid;
if (clientid != 0) {
return next();
}
console.log('CLIENID', clientid);
next(new Error('Authentication error'));
});
// if (socket.clientid != 0) {
// return next();
// }
// // call next() with an Error if you need to reject the connection.
// next(new Error('Authentication error'));
});
答案 0 :(得分:0)
整个过程是异步的。你必须做两件事:
将所有内容移动到事件回调中 为checkAuth函数添加回调或承诺。您还应该转义插入的数据。
编辑解决方案
run
注意:为什么你首先要做2个查询并为用户的每个客户存储密码?
原始回答:
var checkauth = function(auth, cb) {
var rs = 0;
var sql = 'SELECT * FROM clients WHERE pword=?';
var query = connection.query(sql, [auth], (err, client) => {
if(err || !client || client.length < 1) return cb(err || new Error('unkown client'));
sql = 'SELECT * FROM users WHERE pword=?';
query = connection.query(sql, [auth], (err, user) => {
if(err || !user || user.length < 1) return cb(err || new Error('unkown user'));
cb(null, client[0].client_id);
});
});
}