rkusa/koa-passport

how to Capture error in own Strategy

abnerCrack opened this issue · 2 comments

hey:
thanks for your contribution
quesion:

Unable to capture the error
i am run koa-passport-example in local, the auth way use the passport-ldapauth,I think other ways, too
when i close the network and try login, the app was crashed

how to capture the the error ??

events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND corp.xx.com corp.xx.com:389
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

rkusa commented

I had the feeling previously, that providing an async function into a strategy is not a good idea. Your issue proves this feeling.

Since strategies call their callback only as a function, rejected promise results are not catched and there is also no way for koa-passport do catch these rejected promises.

That is, strategies have to be written in the old then/reject approach instead of using async/await:

passport.use(new LocalStrategy(function(username, password, done) {
  fetchUser()
    .then(user => {
      if (username === user.username && password === user.password) {
        done(null, user)
      } else {
        done(null, false)
      }
    })
    .catch(err => done(err))
}))

i think that Not only is your problem,in this example ,I can't even use the promise.

I think the reason should be a passport,because it's a callback.

But I couldn't think of what a better solution

code
//options

const ldapStrategy = require('passport-ldapauth').Strategy
passport.use(new ldapStrategy(options, function(user, done) {
    done(null, user)
}))

//call the method

app.use(route.post('/login',
    passport.authenticate('ldapauth', {
        successRedirect: '/app',
        failureRedirect: '/'
    })
))