javascript - Passport JS successRedirect 在 Node.js 中挂起
问题描述
我正在使用护照local-signup
,可以通过表单创建用户并将页面成功重定向到我指定的页面。
我目前的问题是重定向页面时会挂起。我看到还有其他人经历过类似的事情,但是看着我所拥有的,我无法弄清楚为什么我的例子挂了。
我先对表单进行一些简单的验证,如果可以,我继续进行护照配置:
app.post('/signup', function(req, res, next) {
// Capture form details for when validation fails so can repopulate
var form = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email
}
var first_name = req.body.first_name;
var last_name = req.body.last_name;
var email = req.body.email;
var password = req.body.password;
var password_confirmation = req.body.password_confirmation;
// Validation
req.checkBody('first_name', 'First Name is required').notEmpty();
req.checkBody('last_name', 'Last Name is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password_confirmation', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if (errors) {
res.render('home', {
errors: errors,
form: form
});
}
else {
passport.authenticate('local-signup', {
successRedirect : '/members', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
})(req, res, next);
}
});
local-signup
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) {
// asynchronous
// User.findOne won't fire unless data is sent back
process.nextTick(function(callback) {
// we are checking to see if the user trying to sign up already exists
// User.findOne declared in User Model
User.findOne(email, function(err, isNotAvailable, user) {
if (err) return done(err);
// check to see if theres already a user with that email
if (isNotAvailable == true) {
return done(null, false, { message: 'That email is already taken.' });
} else {
newUser = new Object();
newUser.email = email;
newUser.password = bcrypt.hashSync(password, 10);
pool.query('INSERT INTO users(email, password) VALUES($1, $2) RETURNING *', [newUser.email, newUser.password], function (err, result) {
if(err){
console.log(err);
return console.error('error running query', err);
}
newUser.id = result.rows[0].id;
return done(null, newUser);
});
} // isNotAvailable
}); //User.findOne
});
}));
解决方案
试试这种方式:
passport.authenticate('local', {
successRedirect : '/members',
failureRedirect : '/',
failureFlash : true
})(req, res, next);
推荐阅读
- angular - 如何在 tab-header-pagination 中触发下一个或上一个箭头单击的功能?我想在每个箭头单击时加载下一个或上一个 3 个选项卡
- apache-flink - Flink Logger nullPointException
- listview - 如何使 ListView 在颤动中可点击?
- core-data - 在迭代数组后通过 .sheet 显示模态时显示相同的图像
- flutter - 仅在 dart/flutter 中取十进制数
- php - 如何跟踪 Laravel 框架入口点
- javascript - 用 JS 删除 unselectable="on" ( chrome extension )
- c# - 收集动态程序集:在没有 RunAndCollect 的情况下,从一个程序集移动到多个模块和程序集的内存使用量增加了三倍
- hp-uft - hp-uft:这个应用程序错误日志说什么?
- java - 使用java lambda对列中具有不同值的值进行分组