npm.io
1.2.1 • Published 2 weeks ago

passport-custom

Licence
MIT
Version
1.2.1
Deps
3
Size
10 kB
Vulns
0
Weekly
0

passport-custom

Build

Passport strategy for authenticating with custom logic.

This module lets you authenticate using custom logic in your Node.js applications. It is based on the 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 a strategy

The strategy requires a verify callback containing your authentication logic. The request is always passed as the first argument. Call done with an error, the authenticated user, and optional authentication information.

const passport = require('passport');
const CustomStrategy = require('passport-custom').Strategy;

passport.use(new CustomStrategy(
  function(req, done) {
    User.findOne({
      username: req.body.username
    }, function (err, user) {
      done(err, user);
    });
  }
));

To explicitly pass Passport authentication options to the verify callback, set passOptionsToCallback when constructing the strategy:

passport.use(new CustomStrategy(
  { passOptionsToCallback: true },
  function(req, options, done) {
    // Use request-specific authentication options here.
    done(null, user);
  }
));
Authenticate requests

Use passport.authenticate() with the custom strategy, or the name supplied when registering the strategy.

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
npm run typecheck
npm run typecheck:pack
npm run coverage

Continuous integration enforces 100% line and function coverage and 90% branch coverage on Node.js 24. It also validates the TypeScript declarations from both the repository and an isolated packed consumer, and checks the expected npm package contents.

Credits

License

MIT

Copyright (c) 2014-present Mike Bell

Keywords