护照注册成功回调
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中抽取相关数据。但是我不知道 passport
中的成功回调在哪里
查看 passport documentation 您应该能够通过自己重定向响应来实现此目的。
示例:
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
可用于检查注册是否成功,并根据其值 - 相应地重定向用户。
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中抽取相关数据。但是我不知道 passport
中的成功回调在哪里查看 passport documentation 您应该能够通过自己重定向响应来实现此目的。
示例:
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
可用于检查注册是否成功,并根据其值 - 相应地重定向用户。