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.
npm install passport-strategyVerified import paths — ran on the pinned version, not inferred.
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.
Consider using newer alternatives or explicitly reviewing the base `Strategy` implementation within the currently maintained `passport` package itself or derived strategies. For new projects, ensure compatibility with the `passport` version you are using. There is also `@passport-next/passport-strategy` (v1.1.0, published 2018) which offers a slightly newer base.
For CommonJS, use `const Strategy = require('passport-strategy');` to correctly import the constructor. For ESM/TypeScript, `import Strategy from 'passport-strategy';` or `import { Strategy } from 'passport-strategy';` might be transpiled to work, but direct CommonJS interoperability should use the default import pattern.Always extend `Strategy` and provide your own `authenticate` method in the subclass. For example: `class CustomStrategy extends Strategy { authenticate(req, options) { /* ... */ } }` or `util.inherits(CustomStrategy, Strategy); CustomStrategy.prototype.authenticate = function(...) { /* ... */ };`.Ensure `passport` and `express-session` (if using sessions) are installed and correctly configured in your application. Register your custom strategy using `passport.use(new MyStrategy());` and set up session serialization/deserialization.
In CommonJS, ensure you use `const Strategy = require('passport-strategy');`. In ESM/TypeScript, ensure your transpilation handles `module.exports` correctly, often `import Strategy from 'passport-strategy';` is needed, or potentially `import { Strategy } from 'passport-strategy';` if tooling assumes named export behavior.Create a subclass that extends `Strategy` and provides its own `authenticate(req, options)` method with your authentication logic. `class MyStrategy extends Strategy { authenticate(req, options) { /* ... */ } }`Verify that `app.use(passport.initialize());` and `app.use(passport.session());` (if using sessions) are correctly placed before any routes that require authentication. Also, ensure `passport.serializeUser` and `passport.deserializeUser` are implemented to manage user sessions.
No dependency data recorded yet.