我正在尝试使用护照模块在我的网络应用上建立一个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,但在尝试进行身份验证时收到以下错误消息:
Error: OAuthStrategy requires session support. Did you forget app.use(express.session(...))?
有谁知道我为什么会这样做?
答案 0 :(得分:5)
加载的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')));
});
// ...
答案 1 :(得分:2)
对于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({
...
));
答案 2 :(得分:0)
如果您使用的是最新快递(3.0) 用以下内容替换cookie解析器行:
app.use(express.cookieParser( “thissecretrocks”));
为我做同样的错误!!
答案 3 :(得分:0)
只需将express-session
添加到主js文件中即可。
const session = require('express-session');
// After you declare "app"
app.use(session({ secret: 'melody hensley is my spirit animal' }));
就像这些人为LinkedIn commit做的那样。
显然不要忘记安装express-session
,所以npm install express-session
或将"express-session": "^1.15.3"
添加到您的package.json