Passportjs _userproperty undefined

时间:2014-03-05 03:23:23

标签: express passport.js

我使用Express + Passport建立一个登录和注册的网站。最近一直工作正常,一个错误出现了。它发生在我尝试调用req.login或req.logout时,它们是Passport应该提供的中间件。

TypeError: Cannot read property '_userProperty' of undefined
    at IncomingMessage.req.logout.req.logOut (D:\Workspace\Github\fyp-web\node_modules\passport-local\node_modules\passport-oauth\node_modules\passport\lib\passport\http\request.js:62:41)
    at D:\Workspace\Github\fyp-web\routes\user.js:29:7
    at callbacks (D:\Workspace\Github\fyp-web\node_modules\express\lib\router\index.js:161:37)
    at param (D:\Workspace\Github\fyp-web\node_modules\express\lib\router\index.js:135:11)
    at pass (D:\Workspace\Github\fyp-web\node_modules\express\lib\router\index.js:142:5)
    at Router._dispatch (D:\Workspace\Github\fyp-web\node_modules\express\lib\router\index.js:170:5)
    at Object.router (D:\Workspace\Github\fyp-web\node_modules\express\lib\router\index.js:33:10)
    at next (D:\Workspace\Github\fyp-web\node_modules\express\node_modules\connect\lib\proto.js:190:15)
    at next (D:\Workspace\Github\fyp-web\node_modules\express\node_modules\connect\lib\proto.js:192:9)
    at Object.app.use.express.errorHandler.dumpExceptions [as handle] (D:\Workspace\Github\fyp-web\app.js:143:3)

以下是图书馆出错的一行:

req.logIn = function(user, options, done) {
  .....
  var property = this._passport.instance._userProperty || 'user';

我相信我已经正确配置了中间件,策略和路由部分,因为上次没有错误。但为了安全起见,我会在这里附上这些代码段:

//in app.js
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser(env.server_secret));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.session({ secret:env.server_secret,cookie: { maxAge: 60000 * 60 * 24 }}));
app.use(passport.initialize());
app.use(passport.session());

//in config_passport.js
/* Local strategy */
passport.use(new LocalStrategy(
{
  usernameField: 'email',
  passwordField: 'password'
},
function(email, password, done) {
  Users.isValidUserPassword(email, password, done);
}
));

1 个答案:

答案 0 :(得分:0)

好吧,我自己弄清楚了。

问题是由我在app.js中执行此操作引起的:

//app.js
req._passport = passport;

我希望通过将护照实例附加到所有请求来传递护照实例。它给我带来了麻烦,因为护照本身正在使用" _passport"。如果您查看出错的代码,您会看到:

//request.js in passport node_module/passport/lib/http/request.js 
var property = 'user';
if (this._passport && this._passport.instance) {
  property = this._passport.instance._userProperty || 'user';
}

所以我覆盖了护照的代码。这就是" this._passport"没有定义。

希望这有助于遇到同样问题的其他人。