简体   繁体   English

节点+快递+猫鼬查询身份验证

[英]node + express + mongoose query authentification

Im building a simple web app app in express, and I'm a bit stuck in the authentification for my sessions, im going to validate everything in the client with backbone and regExp eventually when i start building the front end of the app. 我在Express中构建了一个简单的Web应用程序应用程序,我在会话的身份验证中有些卡住,最终在我开始构建应用程序的前端时,我将使用骨干网和regExp来验证客户端中的所有内容。 no basycally i have an issue with the query in mongoose returning a mongoose document object. 没有basycally,我在猫鼬查询返回猫鼬文档对象时遇到了问题。 I've looked up a couple of solutions, in the query you can use a .lean() to get an object, or grab the return from the query and apply a .toObject(), that all works fine and dandy, however when i try and authenticate the value of the key with a string it returns false. 我已经找到了几种解决方案,在查询中可以使用.lean()获取对象,或者从查询中获取返回值并应用.toObject(),但是一切正常,但当我尝试使用返回false的字符串来验证密钥的值。 i'll post an example code, its not exactly what i've got, but its close enough to get the point across. 我将发布一个示例代码,它不完全是我所拥有的,但是它足够接近以至于可以理解这一点。

this would be an example of my models file 这将是我的模型文件的一个示例

var User = new Schema({}, { strict: false });

User.method('authenticate', function(plainText) {
var object = this.toObject();
return plainText == object.password;  });
mongoose.model('User', User);
mongoose.connect( definitions.dbConnect );

and in my app file i would have something like 在我的应用程序文件中,我会看到类似

app.get('/session', routes.showLogin);
app.post('/session/new', routes.login);

and in my routes file id have something like 在我的路线文件ID中有类似

exports.login = function(req, res){


var userQuery = new RegExp(req.body.username , 'i');
var passwordQuery = new RegExp(req.body.password, 'i');     
var Query = User.find();

Query.findOne().where('username', userQuery).exec(function(err, user){

        if(user && user.authenticate((req.body.password))){
                    req.session.userId = user._id;

                }else{

                    res.redirect('/session');
                }
    });
},

Any ideas would be appreciated, its probably very silly... : / 任何想法将不胜感激,它可能非常愚蠢...:/

Thanks in advanced! 提前致谢!

是的,罗伯特·克莱普(Robert klep)的钱是对的,在我查看了mongoose文档和mongodb文档之后,很明显查询返回了mongo文档对象,术语应转换为相同的对象或变量类型,以使操作符起作用。

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

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