我已经能够使用Passport进行身份验证,但我不知道如何访问序列化用户。我已经在几个地方读过访问它的方法是使用req.user但是当我尝试使用GET到/ hello时,它表示用户未定义...
这是我的app.js
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var authentication = require('./routes/authentication');
var content = require('./routes/content');
var posts = require('./routes/post.js');
var upload = require('./routes/upload.js');
var media = require('./routes/media.js');
var http = require('http');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongoose = require('mongoose');
var mongoConnectionString = 'mongodb://localhost/judgmental';
var User = require('./models/user.js');
mongoose.connect(mongoConnectionString, function(error) {
if (error) {
throw error;
}
console.log('Successfully connected to MongoDB');
});
var app = express();
// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('upload_dir', __dirname + '/uploads/posts/');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir:'./uploads/temp'}));
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser('keyboard cat'));
app.use(express.cookieSession({secret:'keyboard cat', maxAge: 1000}));
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
passport.use(new LocalStrategy(
function(username, password, done) {
var query = username.indexOf("@") !== -1 ? {email: username} : {username: username};
User.findOne(query, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Incorrect username.' }); }
user.authenticate(password, function(error, user){
if (error) { return done(error); }
if (!user) { return done(null, false, { message: 'Incorrect password.' }); }
return done(null, user);
});
});
}
));
// routes
// Users
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/content', content.show);
// Posts
app.get('/post', posts.single);
app.get('/post/test/:id', posts.test);
app.get('/posts', posts.list);
app.post('/post/submitPost', posts.submitPost);
app.get('/post/upload', posts.uploadDefault)
// Upload
app.post('/upload/upload-file', upload.upload);
// Image handler
app.get('/media/:id', media.getImage);
// Authentication
app.post('/signUp', authentication.signUp);
app.post('/signIn', authentication.signIn);
app.get('/hello', function(req, res) {
res.render('index.jade', { id: req.user.id });
});
passport.serializeUser(function(user, done) {
console.log("serializeUser");
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
console.log("deserializeUser");
User.findById(id, function(err, user) {
done(err, user);
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
我知道用户是序列化的,因为我在进行身份验证时会记录“serializeUser”。有人知道如何在我的GET / hello中获取序列化用户吗?