0
votes

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..

4

4 Answers

2
votes

you should call a generator function with yield keyword. but you can use yield just in generator functions. so you can wrap your function in co() like bellow:

passport.use("login",new LocalStrategy(function(username, password, done) {
   // retrieve user ...
   co(function *() {
     try {
        var user=yield getUser(username);
        console.log(user);
        return user;
     } catch (ex) {
        return null;
     }
   })(done);
}));
1
votes

You can see this demo: https://github.com/dozoisch/koa-react-full-example/blob/master/lib/authenticator.js

@Mohammad Rasti 's method doesn't work in my project (may be because of version). Following code works for me.

  passport.use(new LocalStrategy(function(username, password, done) {
    co(function*() {
      try {
        var user = yield getUser(username);
        if (username === user.username && password === user.password) {
          return user;
        } else {
          return null;
        }
      } catch(e) {
        return null;
      }
    }).then(function(user) {
      done(null, user);
    });
  }));
1
votes

this is how I'm using it:

var LocalStrategy = require('passport-local').Strategy
passport.use(new LocalStrategy(function(username, password, done) {

  co(function *() {
    try {
      return yield getUser(username, password);
    } catch (ex) {
      console.log('error: ', ex);
      return null;
    }
  }).then(function(user) {
    console.log('found: ', user);
    done(null, user);
  });
}))
0
votes

Hm you just missing a yield in the passport.use() caller function.

passport.use(new LocalStrategy(
  function* (username, password, done) {

    var useri = yield getUser(username);

    console.log(useri);
  }
));

But like other answers, its best to use co for easier handling.