Образцы образца https://scotch.io/tutorials/easy-node-authentication-setup-and-local Я создал приложение для регистрации, чтобы проверить паспорт.Экспресс флеш-сообщения с паспортом не работает
Я использую Express 4 и модуль сотовой связи.
Мои маршруты следующие, то есть на GET /signup
Я показываю форму регистрации с любым возможным сообщением signupMessage
(всегда не определено). На POST /signup
попытка проверки подлинности учетных данных:
router.get('/signup', function(req, res) {
debug('GET signup flash message "%s"',req.flash('signupMessage'));
res.render('signup', { message: req.flash('signupMessage') });
});
// process the signup form
router.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
Моего паспорт код родственного место рядом. Он работает, но строки, подобные return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
, кажется, должны установить сообщение флэш-сообщения запроса, похоже, не работают, потому что флеш-сообщение никогда не отображается в форме регистрации, показанной методом GET.
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
debug("Auth");
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
debug("before findOne");
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
debug("inside findOne");
// if there are any errors, return the error
if (err) {
return done(err);
}
// check to see if theres already a user with that email
if (user) {
debug('user exists');
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
debug('creating new user');
// 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);
// save the user
newUser.save(function(err) {
if (err) {
throw err;
}
return done(null, newUser);
});
}
});
});
}));
После поисках помощи я также пытаюсь поставить:
return done(null, false, {message: 'That email is already taken.'});
и используется в GET /signup
метод:
router.get('/signup', function(req, res) {
res.render('signup', { message: req.flash('error') });
});
но это не работает.
Кто-нибудь может мне помочь?
Спасибо.
Кажется, что вы ответили на свою собственную проблему, и это здорово! Вместо того, чтобы редактировать ваш вопрос, обычно предпочтительнее, если вы опубликуете решение в качестве ответа, так что другие люди, которые имеют такую же проблему, смогут легче найти его в будущем. – ttarik