我正在使用 Passport.js 构建一个使用 轻松节点身份验证: 安装和本地教程的身份验证系统。
我对 passport.session()
的作用感到困惑。
在使用了不同的中间件之后,我开始明白了 express.session()
是通过 cookie 向客户端发送会话 ID 的,但是我对 passport.session()
是做什么的以及除了 express.session()
之外为什么还需要它感到困惑。
下面是我如何设置我的应用程序:
//Server.js 配置应用程序并设置 webserver
//importing our modules
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var configDB = require('./config/database.js');
//Configuration of Databse and App
mongoose.connect(configDB.url); //connect to our database
require('./config/passport')(passport); //pass passport for configuration
app.configure(function() {
//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 info from html forms
app.set('view engine', 'ejs'); //set up ejs for templating
//configuration for passport
app.use(express.session({ secret: 'olhosvermelhoseasenhaclassica', maxAge:null })); //session secret
app.use(passport.initialize());
app.use(passport.session()); //persistent login session
app.use(flash()); //use connect-flash for flash messages stored in session
});
//Set up routes
require('./app/routes.js')(app, passport);
//launch
app.listen(port);
console.log("Server listening on port" + port);