{"record":{"id":"68fbe463ba43e979","repo":"jaredhanson/passport","slug":"authentication-strategies-must-have-a-name","errorCode":null,"errorMessage":"Authentication strategies must have a name","messagePattern":"Authentication strategies must have a name","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/authenticator.js","lineNumber":62,"sourceCode":" * @param {string} [name=strategy.name] - Name of the strategy.  When specified,\n *          this value overrides the strategy's name.\n * @param {Strategy} strategy - Authentication strategy.\n * @returns {this}\n *\n * @example <caption>Register strategy.</caption>\n * passport.use(new GoogleStrategy(...));\n *\n * @example <caption>Register strategy and override name.</caption>\n * passport.use('password', new LocalStrategy(function(username, password, cb) {\n *   // ...\n * }));\n */\nAuthenticator.prototype.use = function(name, strategy) {\n  if (!strategy) {\n    strategy = name;\n    name = strategy.name;\n  }\n  if (!name) { throw new Error('Authentication strategies must have a name'); }\n  \n  this._strategies[name] = strategy;\n  return this;\n};\n\n/**\n * Deregister a strategy that was previously registered with the given name.\n *\n * In a typical application, the necessary authentication strategies are\n * registered when initializing the app and, once registered, are always\n * available.  As such, it is typically not necessary to call this function.\n *\n * @public\n * @param {string} name - Name of the strategy.\n * @returns {this}\n *\n * @example\n * passport.unuse('acme');","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/jaredhanson/passport/blob/217018dbc46dcd4118dd6f2c60c8d97010c587f8/lib/authenticator.js#L44-L80","documentation":"Passport's Authenticator#use() registers a strategy under a name. If no name is supplied, Passport falls back to the strategy instance's own .name property (e.g. the name passed to the strategy's constructor). If both are missing/undefined, it throws this error because strategies are keyed by name and cannot be looked up later.","triggerScenarios":"calling passport.use(new SomeStrategy({...})) where the strategy instance has no .name property, or passport.use(undefined/null) e.g. due to a failed import or a factory returning undefined.","commonSituations":"Typo'd require/import so the strategy constructor is undefined; using a custom strategy class that never calls super(name) or sets this.name; upgrading a strategy package where the constructor signature changed and the name argument was dropped.","solutions":["Pass an explicit name as the first argument: passport.use('local', new LocalStrategy(...))","Ensure the strategy instance exposes a .name property (most official strategies set it in their constructor)","Log/inspect the value passed to passport.use to confirm the import is not undefined","Verify the strategy package version and constructor API match the docs"],"exampleFix":"// before\nconst LocalStrategy = require('passport-local').Strategy;\npassport.use(new LocalStrategy(verify)); // LocalStrategy undefined -> no .name\n// after\nconst LocalStrategy = require('passport-local').Strategy;\npassport.use('local', new LocalStrategy(verify));","handlingStrategy":"validation","validationCode":"function validateStrategyRegistration(name, strategy) {\n  const s = strategy || name;\n  const n = strategy ? name : (s && s.name);\n  if (typeof n !== 'string' || n.length === 0) {\n    throw new TypeError('Strategy must have a name before passport.use()');\n  }\n}","typeGuard":"function hasStrategyName(s) {\n  return typeof s === 'object' && s !== null && typeof s.name === 'string' && s.name.length > 0;\n}","tryCatchPattern":"try {\n  passport.use(new LocalStrategy(opts, verify));\n} catch (err) {\n  if (err.message.includes('strategies must have a name')) {\n    passport.use('local', new LocalStrategy(opts, verify));\n  } else { throw err; }\n}","preventionTips":["Always pass an explicit name string as the first argument to passport.use()","Sanity-check strategy imports (undefined imports produce nameless instances)","Write a startup smoke test that registers all strategies and fails fast","Pin strategy package versions and review changelogs when upgrading"],"tags":["passport","strategy-registration","configuration"],"backgroundTag":"strategy-name-missing","analyzedSha":"217018dbc46dcd4118dd6f2c60c8d97010c587f8","analyzedAt":"2026-08-31T21:59:48.726Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}