Skip to content

Node + Passport, Error: Authentication strategies must have a name

I’m trying to initialize a SAML strategy during the require line. Something like this:

var myStrat = new require('passport-something').Strategy(
    { .... }
);
passport.use('something', myStrat);

but am getting the error:

.../passport/lib/authenticator.js:54
  if (!name) { throw new Error('Authentication strategies must have a name'); }
               ^

Error: Authentication strategies must have a name
    at Authenticator.use ...

or TypeError: Cannot read property 'name' of undefined at Authenticator.use if a custom strategy name is not defined: passport.use(myStrat);

.

I’ve had it like this (which works):

var mySomething = require('passport-something');
var myStrat = new mySomething.Strategy(
    { .... }
);
passport.use(myStrat);

but I wish to change it because I need to call passport-saml’s Stragety.generateServiceProviderMetadata() function later on. Which (I think) mean I need a variable pointing to the new Strategy instance. Not a big deal I know, just would like to have the code for this particular strategy look more in line with the rest if I can. Which all look like:

var GoogleStrat = require( 'passport-google-oauth2' ).Strategy;
passport.use('google', new GoogleStrat(
    ....
));

Answer

this should work:

var myStrat = require('passport-something').Strategy(
    { .... }
);
passport.use('something', new myStrat());

or, if you want to hold the instance:

var myStratInstance = new (require('passport-something').Strategy)(
    { .... }
);
passport.use('something', myStratInstance);