diff --git a/README.md b/README.md index 6f74b6a..e8f1978 100644 --- a/README.md +++ b/README.md @@ -29,14 +29,30 @@ The strategy requires a `verify` callback, which is where the custom logic goes `done` providing a user. Note that, req is always passed as the first parameter to the `verify` callback. -Here is the pseudo code. - - passport.use('strategy-name', new CustomStrategy( - function(req, callback) { - // Do your custom user finding logic here, or set to false based on req object - callback(null, user); - } - )); +Here is the pseudo code: + +``` +passport.use('strategy-name', new CustomStrategy( + function(req, callback) { + // Do your custom user finding logic here, or set to false based on req object + callback(null, user); + } +)); +``` + +And a basic example: + +``` +passport.use(new CustomStrategy( + function(req, done) { + User.findOne({ + username: req.body.username + }, function (err, user) { + done(err, user); + }); + } +)); +``` #### Authenticate Requests @@ -46,11 +62,14 @@ authenticate requests. For example, as route middleware in an [Express](http://expressjs.com/) application: - app.post('/login', - passport.authenticate('custom', { failureRedirect: '/login' }), - function(req, res) { - res.redirect('/'); - }); +``` +app.post('/login', + passport.authenticate('custom', { failureRedirect: '/login' }), + function(req, res) { + res.redirect('/'); + } +); +``` ## Tests diff --git a/lib/strategy.js b/lib/strategy.js index f3e20d4..1d745b7 100644 --- a/lib/strategy.js +++ b/lib/strategy.js @@ -12,8 +12,10 @@ var passport = require('passport-strategy'), * * Examples: * passport.use(new CustomStrategy( - * function(done) { - * User.findOne({ uid: 1 }, function (err, user) { + * function(req, done) { + * User.findOne({ + * username: req.body.username + * }, function (err, user) { * done(err, user); * }); * } diff --git a/package.json b/package.json index 87d74c0..7251f1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "passport-custom", - "version": "1.0.3", + "version": "1.0.4", "description": "Custom authentication strategy for Passport.", "keywords": [ "passport", diff --git a/test/strategy.error.test.js b/test/strategy.error.test.js index 953a28a..9720ace 100644 --- a/test/strategy.error.test.js +++ b/test/strategy.error.test.js @@ -6,7 +6,7 @@ var chai = require('chai'), describe('Strategy', function () { describe('encountering an error during verification', function () { - var strategy = new Strategy(function (done) { + var strategy = new Strategy(function (req, done) { done(new Error('something went wrong')); }); @@ -31,7 +31,7 @@ describe('Strategy', function () { }); describe('encountering an exception during verification', function () { - var strategy = new Strategy(function (done) { + var strategy = new Strategy(function (req, done) { throw new Error('something went horribly wrong'); }); diff --git a/test/strategy.fail.test.js b/test/strategy.fail.test.js index 676c894..b284cf8 100644 --- a/test/strategy.fail.test.js +++ b/test/strategy.fail.test.js @@ -7,7 +7,7 @@ var chai = require('chai'), describe('Strategy', function () { describe('failing authentication', function () { - var strategy = new Strategy(function (done) { + var strategy = new Strategy(function (req, done) { return done(null, false); }); @@ -31,7 +31,7 @@ describe('Strategy', function () { }); describe('failing authentication with info', function () { - var strategy = new Strategy(function (done) { + var strategy = new Strategy(function (req, done) { return done(null, false, { message: 'authentication failed' }); }); diff --git a/test/strategy.normal.test.js b/test/strategy.normal.test.js index c07aaba..16c8ee3 100644 --- a/test/strategy.normal.test.js +++ b/test/strategy.normal.test.js @@ -7,7 +7,7 @@ var chai = require('chai'), describe('Strategy', function () { describe('handling a request with valid credentials in query', function () { - var strategy = new Strategy(function (done) { + var strategy = new Strategy(function (req, done) { return done(null, { id: '1234' }, { scope: 'read' }); });