我正在使用Passport.js使用“ 简易节点身份验证:设置和本地”教程构建身份验证系统。
我对做什么感到困惑passport.session()
。
在使用了不同的中间件之后,我逐渐了解到这express.session()
是通过cookie向客户端发送会话ID的原因,但是passport.session()
除了之外,我对做什么以及为什么需要它感到困惑express.session()
。
这是我设置应用程序的方式:
// Server.js配置应用程序并设置Web服务器
//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);