pc

passport-custom

Custom logic authentication strategy for Passport and Node.js.

Showing:

Popularity

Downloads/wk

56.9K

GitHub Stars

91

Maintenance

Last Commit

7mos ago

Contributors

10

Package

Dependencies

1

Size (min+gzip)

0.4KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

passport-custom

Build Coverage Status Quality Dependencies

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) {
    // 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

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial