I'm having some troubles with Koa, Passport and Monk.
I'd like to have a simple local authentication with Passport. I've followed some tutorials and got as far as this: (auth.js)
const
passport = require('koa-passport'),
LocalStrategy = require('passport-local').Strategy,
monk = require('monk'),
wrap = require('co-monk'),
db = monk('localhost/try'),
users = wrap(db.get('users'));
var user = {
id: 1,
username: 'test'
};
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
done(null, user);
});
passport.use(new LocalStrategy(
function(username, password, done) {
if (username === 'test' && password === 'test') {
return done(null, user);
} else {
return done(null, false);
}
}
));
module.exports = passport;
Now this works ok, but how can I use my MongoDb database here instead of if (username === 'test' && password === 'test')?
I've tried to add this function:
function *getUser(name) {
var useri = yield users.find({name:name});
return useri;
};
and then use it like this:
passport.use(new LocalStrategy(
function(username, password, done) {
var useri = getUser(username);
console.log(useri);
if (username === 'test' && password === 'test') {
return done(null, user);
} else {
return done(null, false);
}
}
));
but only end up getting {} in my console.
So how do I do this? It's all so easy in Express but with this Koa thingy I'm really struggling to understand how it all works..