Skip to content

Commit

Permalink
allow calling authenticate without callback #125
Browse files Browse the repository at this point in the history
  • Loading branch information
rkusa committed Dec 20, 2018
1 parent 562aa13 commit f3e1708
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions lib/framework/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ function authenticate(passport, name, options, callback) {
callback.reject(err)
}
}
} else {
// support the inline usage of `passport.authenticate` without a callback, e.g.
// ```
// const [user] = await passport.authenticate('jwt')(ctx)
// ```
// In this case we want to forward both the error and result to the returned promise.
callback = function(err, user, info, status) {
if (err) {
return callback.reject(err)
} else {
return callback.resolve([user, info, status])
}
}
}

const middleware = promisify(_authenticate(passport, name, options, callback))
Expand Down Expand Up @@ -142,11 +155,17 @@ function authenticate(passport, name, options, callback) {
middleware(req, res).then(resolve, reject)
})

return p.then(cont => {
// cont equals `false` when `res.redirect` or `res.end` got called
// in this case, call next to continue through Koa's middleware stack
if (cont !== false) {
return next()
return p.then(result => {
if (next) {
// cont equals `false` when `res.redirect` or `res.end` got called
// in this case, call next to continue through Koa's middleware stack
if (result !== false) {
return next()
}
} else {
// when not providing a callback to `passport.authenticate` the result of that usually goes
// into the callback is returned from the Promise instead
return result
}
})
}
Expand Down

0 comments on commit f3e1708

Please sign in to comment.