koa-passport
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -5,2 +5,3 @@ /** | ||
var passport = require('passport') | ||
var co = require('co') | ||
@@ -44,7 +45,7 @@ /** | ||
/** | ||
* assport's authenticate middleware for Koa. | ||
* Passport's authenticate middleware for Koa. | ||
* | ||
* @param {String|Array} name | ||
* @param {Object} options | ||
* @param {Function} callback | ||
* @param {GeneratorFunction} callback | ||
* @return {GeneratorFunction} | ||
@@ -54,2 +55,29 @@ * @api private | ||
function authenticate(passport, name, options, callback) { | ||
// normalize arguments | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
options = options || {} | ||
if (callback) { | ||
if (callback.constructor.name !== 'GeneratorFunction') { | ||
throw TypeError('Your custom authentication callback must be a Generator Function') | ||
} | ||
// When the callback is set, neither `next`, `res.redirect` or `res.end` | ||
// are called. That is, a workaround to catch the `callback` is required. | ||
// The `passportAuthenticate()` method below will therefore set | ||
// `callback.done`. Then, once the authentication finishes, the modified | ||
// callback yields the original one and afterwards triggers `callback.done` | ||
// to inform `passportAuthenticate()` that we are ready. | ||
var _callback = callback | ||
callback = function callback(err, user, info) { | ||
co(function*() { | ||
yield _callback(err, user, info) | ||
callback.done(null, false) | ||
})() | ||
} | ||
} | ||
var middleware = _authenticate(passport, name, options, callback) | ||
@@ -81,2 +109,6 @@ return function* passportAuthenticate(next) { | ||
if (callback) { | ||
callback.done = done | ||
} | ||
// call the connect middleware | ||
@@ -83,0 +115,0 @@ middleware(ctx.req, res, done) |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Passport middleware for Koa", | ||
@@ -19,2 +19,3 @@ "keywords": [ | ||
"dependencies": { | ||
"co": "^3.0.6", | ||
"passport": ">=0.2.x" | ||
@@ -21,0 +22,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7382
126
2