What is passport-custom?
The passport-custom npm package allows you to create custom authentication strategies for Passport.js. This is useful when you need to implement authentication mechanisms that are not covered by the existing Passport.js strategies.
What are passport-custom's main functionalities?
Custom Authentication Strategy
This feature allows you to define a custom authentication strategy. In this example, the strategy checks for a user in the database and verifies the password.
const passport = require('passport');
const CustomStrategy = require('passport-custom').Strategy;
passport.use('custom', new CustomStrategy(
function(req, done) {
User.findOne({ username: req.body.username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!user.verifyPassword(req.body.password)) { return done(null, false); }
return done(null, user);
});
}
));
Integration with Express
This feature demonstrates how to integrate the custom authentication strategy with an Express application. The '/login' route uses the custom strategy for authentication.
const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(passport.initialize());
app.post('/login',
passport.authenticate('custom', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
Other packages similar to passport-custom
passport-local
The passport-local package is a Passport.js strategy for authenticating with a username and password. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for local authentication using a username and password.
passport-oauth2
The passport-oauth2 package is a Passport.js strategy for authenticating with OAuth 2.0. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for OAuth 2.0 authentication.
passport-http
The passport-http package is a Passport.js strategy for HTTP Basic and Digest authentication. It is similar to passport-custom in that it allows for custom authentication logic, but it is specifically designed for HTTP Basic and Digest authentication.
passport-custom
Passport strategy for authenticating with custom logic.
This module lets you authenticate using custom logic in your Node.js
applications. It is based on passport-local module by Jared Hanson.
By plugging into Passport, custom authentication can be easily and
unobtrusively integrated into any application or framework that supports
Connect-style middleware, including
Express.
Install
$ npm install passport-custom
Usage
Configure Strategy
The custom authentication strategy authenticates users by custom logic of your choosing.
The strategy requires a verify
callback, which is where the custom logic goes and calls
done
providing a user. Note that, req is always passed as the first parameter to the
verify
callback.
Here is the pseudo code:
import passportCustom from 'passport-custom';
const CustomStrategy = passportCustom.Strategy;
passport.use('strategy-name', new CustomStrategy(
function(req, callback) {
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
Use passport.authenticate()
, specifying the 'custom'
strategy (or whatever you named the strategy upon registration), to
authenticate requests.
For example, as route middleware in an Express
application:
app.post('/login',
passport.authenticate('custom', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);
Tests
$ npm install
$ npm test
Credits
License
The MIT License
Copyright (c) 2014-2015 Mike Bell