passportjs出错:未知的身份验证策略“本地”

时间:2014-01-30 10:47:18

标签: node.js passport.js

我正在开发一个nodejs应用程序并使用passportjs进行身份验证。我正在使用当地的护照策略。但是当我尝试登录时,我收到了以下错误:

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

这是我的passport-config.js文件:

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

以下是我的RegistrationsController.js文件,我的身份验证api所在的文件,

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

自从过去2天以来一直在查看代码,但还无法找出错误。我已经安装了passportpassport-local个模块。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:9)

此代码中存在语法错误。 usernameField: 'emailAddress'必须像这样在LocalStrategy构造函数中传递...

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

答案 1 :(得分:0)

这发生在我身上,因为即使我的app.ts文件正在导入护照配置,我也没有将其分配给任何路由,因此该文件从未被解析过。我在控制器中使用它来验证POST /login

免责声明 - 我不确定是否"解析"是正确使用的单词,但似乎是这种情况,因为该文件中的console.log('xfngr')在我放入路径之后才输出。

<强> app.ts

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

我添加了一个使用passportConfig中的属性的路线,它又开始工作了。

<强> app.ts

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

答案 2 :(得分:0)

执行npm install --save passport-local 然后在app.js中加入这一行var LocalStrategy = require("passport-local"); 然后使用此中间件:

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