Error with passportjs: Unknown authentication strategy “local”

拟墨画扇 提交于 2019-12-09 14:58:24

问题


I am developing a nodejs application and using passportjs for authentication. I am using local strategy of passport. But when I try to login, I am getting following error:

Error: Unknown authentication strategy "local"
    at attempt (/home/project/node_modules/passport/lib/middleware/authenticate.js:166:37)
    at authenticate (/home/project/node_modules/passport/lib/middleware/authenticate.js:338:7)
    at exports.authenticate (/home/project/controllers/RegistrationsController.js:87:4)
    at callbacks (/home/project/node_modules/express/lib/router/index.js:164:37)
    at param (/home/project/node_modules/express/lib/router/index.js:138:11)
    at pass (/home/project/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/home/project/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/home/project/node_modules/express/lib/router/index.js:33:10)
    at next (/home/project/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at /home/project/node_modules/express-flash/lib/express-flash.js:31:7

Here is my passport-config.js file:

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});

passport.use({usernameField: 'emailAddress'}, new LocalStrategy(function(username, password, done) {
  User.findOne({ emailAddress: username }, function(err, user) {
    if(err){
        return done(err);
    }
    if (!user) {
        return done(null, false, { message: 'Email ' + username + ' not found'});
    }
    else{
        //check if password matches and pass parameters in done accordingly
         }
     });
}));

And following is my RegistrationsController.js file, where my authenticate api resides,

var passport = require('passport');

exports.authenticate = function(req, res, next){
    console.log('Login request!');
    passport.authenticate('local', function(err, user, info) {
        console.log('In authenticate callback!');
    if (err) return next(err);

    if (!user) {
      req.flash('errors', { msg: info.message });
      res.status(500).json({message: info.message});
    }
      res.json(user);
    })(req, res, next);
}

Have been looking at the code since past 2 days but could not figure out the error yet. I have installed both passport and passport-local modules. Any help would be greatly appreciated.


回答1:


There is a syntax error in this code. usernameField: 'emailAddress' Has to be passed in LocalStrategy constructor like this...

passport.use(new LocalStrategy({
   usernameField: 'email'
}, yourAuthenticateFunction));



回答2:


This was happening to me because even though my app.ts file was importing the passport config, I didn't have it assigned to any routes, so the file was never parsed. I was using it inside a controller to authenticate a POST /login.

Disclaimer - I'm not sure if "parsed" is the correct word to use, but it seemed to be the case because a console.log('xfngr') in that file didn't output until after I put the route in.

app.ts

...
import * as passportConfig from "./config/passport";
...
...

I added a route that was using a property from the passportConfig and it started working again.

app.ts

...
import * as passportConfig from "./config/passport";
...
...
...
app.get("/account", passportConfig.isAuthenticated, userController.getAccount);



回答3:


do npm install --save passport-local then in app.js include this line var LocalStrategy = require("passport-local"); then use this middleware:

app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());


来源:https://stackoverflow.com/questions/21454254/error-with-passportjs-unknown-authentication-strategy-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!