[英]passport register success callback
router.post('/register', function(req, res, next){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
req.checkBody('name', 'Name field is required').notEmpty();
req.checkBody('email', 'Email field is required').notEmpty();
req.checkBody('email', 'Email must be a valid email address').isEmail();
req.checkBody('username', 'Username field is required').notEmpty();
req.checkBody('password', 'Password field is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register');
} else {
passport.authenticate('local-register',{
successRedirect: '/dashboard',
failureRedirect: '/register'
})(req, res, next)
}
});
用戶注冊后,我想執行更多操作,例如在db中輸入相關數據。 但是我不知道護照上的成功回調在哪里
查看護照文件,您應該可以自己重定向響應來實現此目的。
例:
app.post('/login',
passport.authenticate('local'),
function(req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/users/' + req.user.username);
});
因此,您的代碼可能會被修改為類似這樣的內容...
router.post('/register', function(req, res, next){
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
req.checkBody('name', 'Name field is required').notEmpty();
req.checkBody('email', 'Email field is required').notEmpty();
req.checkBody('email', 'Email must be a valid email address').isEmail();
req.checkBody('username', 'Username field is required').notEmpty();
req.checkBody('password', 'Password field is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors){
res.render('register');
} else {
passport.authenticate('local-register',{ })(req, res, function(returnCode) {
// Yay successfully registered user
// Do more stuff
console.log(returnCode); // Check what this value is and redirect accordingly.
res.redirect('/dashboard/'); // How redirect can potentially be done
})
}
});
我不確定returnCode
值是什么,因為這將取決於local-register
策略的實現。
無論如何, returnCode
可以潛在地用於檢查注冊是否成功,並根據其值-相應地重定向用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.