Passport Strategy Base Class

1.0.0 · maintenance · verified Sun Apr 19

The `passport-strategy` package provides an abstract `Strategy` class that serves as the foundational interface for implementing concrete authentication strategies within the Passport.js ecosystem. It defines the core API, including the `authenticate()` method and helper functions like `success()`, `fail()`, `redirect()`, `pass()`, and `error()`, which custom strategies must implement or utilize to manage the authentication flow. While the core `passport` package is actively maintained (latest version ~0.7.0 as of late 2023), the `passport-strategy` package itself, version 1.0.0, has not seen updates since 2013, making it a very stable but effectively unmaintained base. Developers primarily interact with this module by extending its `Strategy` class to create custom authentication logic (e.g., `passport-local`, `passport-github`). Its key differentiator is its role as the common contract for all Passport strategies, enabling a highly modular and extensible authentication system for Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a custom authentication strategy by subclassing the `Strategy` abstract class, overriding its `authenticate` method, and registering it with Passport.js. It shows a simplified 'always success' scenario and how the base class's success method is called.

const util = require('util');
const Strategy = require('passport-strategy');

/**
 * A simple custom strategy extending passport-strategy.
 * This example demonstrates a basic 'always success' strategy.
 */
function CustomAlwaysSuccessStrategy(options) {
  Strategy.call(this);
  this.name = 'custom-always-success';
  this._options = options || {};
}

util.inherits(CustomAlwaysSuccessStrategy, Strategy);

/**
 * The authenticate method must be overridden by subclasses.
 * It performs the actual authentication logic.
 */
CustomAlwaysSuccessStrategy.prototype.authenticate = function(req, options) {
  // In a real strategy, you would parse credentials from req,
  // interact with a database, call an external API, etc.
  // For this example, we simply succeed immediately with a dummy user.
  const user = { id: '123', name: 'Test User' };
  const info = { message: 'Authentication successful with custom strategy.' };

  // Call one of the action functions provided by the base Strategy class.
  // This indicates the outcome of the authentication attempt.
  this.success(user, info);

  // Other possible outcomes:
  // this.fail({ message: 'Invalid credentials' }, 401);
  // this.redirect('/login', 302);
  // this.pass(); // defer to next strategy
  // this.error(new Error('Something went wrong'));
};

// Example usage with Passport.js (requires 'passport' package)
const passport = require('passport');
passport.use(new CustomAlwaysSuccessStrategy());

// To test, imagine an Express route:
// app.get('/auth/custom', passport.authenticate('custom-always-success', { session: false }), (req, res) => {
//   res.json({ message: 'Logged in!', user: req.user });
// });

// For demonstration, let's manually 'authenticate' (without Express context)
// In a real app, this would be handled by Passport middleware.
const dummyReq = {}; // Simulate an Express request object
const strategyInstance = new CustomAlwaysSuccessStrategy();
strategyInstance.authenticate(dummyReq, {}, (err, user, info) => {
  if (user) {
    console.log('Authentication successful:', user, info);
  } else {
    console.error('Authentication failed:', err || info);
  }
});

view raw JSON →