nodejs / passport – 错误:OAuthStrategy需要会话支持

我正在尝试使用护照模块在我的web应用程序上build立一个twitter连接

我已经实施如下:

App.js文件:

/** * Module dependencies. */ var express = require('express'); var user = require('./server/routes/user'); var http = require('http'); var path = require('path'); var app = express(); var passport = require('passport'); var flash = require('connect-flash'); require('./server/config/passport')(passport); // pass passport for configuration // all environments app.set('port', process.env.PORT || 3000); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.configure(function() { app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(app.router); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'))); // set up our express application app.use(express.logger('dev')); // log every request to the console app.use(express.cookieParser()); // read cookies (needed for auth) app.use(express.bodyParser()); // get information from html forms app.set('view engine', 'ejs'); // set up ejs for templating // required for passport app.use(express.session({ secret: 'SECRET' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session }); // // routes ====================================================================== require('./server/routes/index')(app, passport); // load our routes and pass in our app and fully configured passport 

我正在使用express.session但尝试进行身份validation时遇到以下错误消息:

 Error: OAuthStrategy requires session support. Did you forget app.use(express.session(...))? 

有没有人有一个想法,为什么我得到这个?

       

网上收集的解决方案 "nodejs / passport – 错误:OAuthStrategy需要会话支持"

加载的Express中间件的顺序不正确。 尝试这个:

 // ... app.configure(function() { app.use(express.favicon()); app.use(express.logger('dev')); // The following two middlwares are NOT necessary because bodyParser includes them. // app.use(express.json()); // app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(express.cookieParser()); // read cookies (needed for auth) app.use(express.bodyParser()); // get information from html forms // required for passport app.use(express.session({ secret: 'SECRET' })); // session secret app.use(passport.initialize()); app.use(passport.session()); // persistent login sessions app.use(flash()); // use connect-flash for flash messages stored in session app.use(app.router); app.use(require('stylus').middleware(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public'))); }); // ... 

对于4.0这个变化了一下。

首先,您需要安装express-session ,然后在定义路由之前:

 // Authentication configuration app.use(session({ resave: false, saveUninitialized: true, secret: 'bla bla bla' })); 

你可以后面你做OAuth的事情:

 // Passport app.use(passport.initialize()); app.use(passport.session()); passport.use(new TwitterStrategy({ ... )); 

如果您正在使用最新的express(3.0),请将cookie分析器行replace为以下内容:

app.use(express.cookieParser( “thissecretrocks”));

为我工作相同的错误!