{"library":"passport-strategy","title":"Passport Strategy Base Class","description":"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.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install passport-strategy"],"cli":null},"imports":["import { Strategy } from 'passport-strategy';","const Strategy = require('passport-strategy');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const util = require('util');\nconst Strategy = require('passport-strategy');\n\n/**\n * A simple custom strategy extending passport-strategy.\n * This example demonstrates a basic 'always success' strategy.\n */\nfunction CustomAlwaysSuccessStrategy(options) {\n  Strategy.call(this);\n  this.name = 'custom-always-success';\n  this._options = options || {};\n}\n\nutil.inherits(CustomAlwaysSuccessStrategy, Strategy);\n\n/**\n * The authenticate method must be overridden by subclasses.\n * It performs the actual authentication logic.\n */\nCustomAlwaysSuccessStrategy.prototype.authenticate = function(req, options) {\n  // In a real strategy, you would parse credentials from req,\n  // interact with a database, call an external API, etc.\n  // For this example, we simply succeed immediately with a dummy user.\n  const user = { id: '123', name: 'Test User' };\n  const info = { message: 'Authentication successful with custom strategy.' };\n\n  // Call one of the action functions provided by the base Strategy class.\n  // This indicates the outcome of the authentication attempt.\n  this.success(user, info);\n\n  // Other possible outcomes:\n  // this.fail({ message: 'Invalid credentials' }, 401);\n  // this.redirect('/login', 302);\n  // this.pass(); // defer to next strategy\n  // this.error(new Error('Something went wrong'));\n};\n\n// Example usage with Passport.js (requires 'passport' package)\nconst passport = require('passport');\npassport.use(new CustomAlwaysSuccessStrategy());\n\n// To test, imagine an Express route:\n// app.get('/auth/custom', passport.authenticate('custom-always-success', { session: false }), (req, res) => {\n//   res.json({ message: 'Logged in!', user: req.user });\n// });\n\n// For demonstration, let's manually 'authenticate' (without Express context)\n// In a real app, this would be handled by Passport middleware.\nconst dummyReq = {}; // Simulate an Express request object\nconst strategyInstance = new CustomAlwaysSuccessStrategy();\nstrategyInstance.authenticate(dummyReq, {}, (err, user, info) => {\n  if (user) {\n    console.log('Authentication successful:', user, info);\n  } else {\n    console.error('Authentication failed:', err || info);\n  }\n});\n","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}