简体   繁体   中英

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. no basycally i have an issue with the query in mongoose returning a mongoose document object. 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. 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

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文档对象,术语应转换为相同的对象或变量类型,以使操作符起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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