最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node + Passport, Error: Authentication strategies must have a name - Stack Overflow

programmeradmin1浏览0评论

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(
    ....
));

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(
    ....
));
Share Improve this question edited Feb 9, 2016 at 21:51 House3272 asked Feb 9, 2016 at 21:36 House3272House3272 1,0376 gold badges14 silver badges30 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 7

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);

in NEST.JS don't forget to check, from where did you import the Strategy

Example:

import {Strategy} from 'passport-local' ===> true

import {Strategy} from 'passport' ===> false

// provide a name for your strategy like this in this portion of your code

import { PassportStrategy } from "@nestjs/passport"; export class JwtAuthStrategy extends PassportStrategy(Strategy, ''){

}

// in nest js

I got this error. if you got this line of code in your code

"passport.use(user.createStrategy);"

make sure to change to this.,

passport.use(user.createStrategy());

hope this is useful for you!

If you have this line of code:

passport.use(User.createStrategy);

Then Change it to:

passport.use(User.createStrategy());

I solve the problem like this First you defined on the top

const LocalStrategy = require('passport-local').Strategy;

then you change the code

passport.use(User.authenticate());

to

passport.use(new LocalStrategy(User.authenticate()));
发布评论

评论列表(0)

  1. 暂无评论