我正在构建SPA:Backbone.js,Node.js,Mongo。我正在使用Passport.js进行身份验证,并想知道如何将req.flash变量暴露给我的Handlebars模板?在这种情况下,如果用户尝试使用收到的电子邮件登录,我想在我的视图中公开flash变量“signupMessage”。
在我的Express服务器中:
app.use(function(req, res, next){
res.locals.flash = req.flash
next();
});
护照本地策略:
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
petnameField : 'petname',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
process.nextTick(function() {
User.findOne({ 'local.email' : email }, function(err, user) {
if (err)
return done(err);
// check to see if theres already a user with that email
if (user) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
console.log('need to update now');
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.local.email = email;
newUser.local.password = newUser.generateHash(password);
newUser.local.petname = req.body.petname;
newUser.kittenType = req.body.kittenType;
// save the user
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));