kraken-example-with-passport
kraken-example-with-passport copied to clipboard
Can't use mongoose to populate in localStrategy
So i just ran into a weird issue where in my schema for user i have a column that has a ref
set to Purchases
. Like the code below
purchases: [{ type: Schema.Types.ObjectId, ref: 'Purchase' }],
Within my lib/auth.js
file when calling User.findOne
i'm also calling populate('purchases') which i noticed is pulling in the purchases since i can console.log(user.purchases) fine within
.exec()`
// Helper method to retrieve user from a local DB to ensure the provided password matches
exports.localStrategy = function() {
return new LocalStrategy(function(username, password, done) {
// Retrieve the user from the database
User.findOne({ login: username })
.populate('purchases')
.exec(function(err, user) {
console.log('user.purchases', user.purchases);
if (err) return done(err);
if (!user) {
return done(null, false, {
message: 'User not found'
});
}
if (!user.passwordMatches(password)) {
return done(null, false, {
message: 'Incorrect Password'
});
}
// If everything passes, return the retrieved user object
done(null, user);
});
});
};
but for some reason when i use res.user
within my controller or anywhere else it doesn't have the purchases populated it only shows the object id of the purchase. Is there something i'm missing or doing wrong? Let me know if you need more context. Thanks!