0

我应该重定向还是呈现页面?身份验证的最佳做法是什么。

var User = require('models/user.js');
        User.authenticate(req.body.email, req.body.password)
            .then(function(error, user){
                //IF SUCCES AND NO ERROR
                res.redirect('/profile');
                //OR
                res.render('profile.pug');
            })
            .catch(error => console.log(error));
4

1 回答 1

0

在您的路线中,您应该这样做:

当一切正常(没有错误):

return res.render('view',{
    param1:param2
});

出现错误时:

req.flash('error', error.message); // If you are using flash
return res.redirect('back');      // It will redirect user back
                                 // and display some error message

如果您要编写用于身份验证的中间件:

middlewareObj.isAuthenticated = function(req, res, next) {
    if (req.isAuthenticated()){
        return next();
    } else {
        return res.redirect('/signin'); // Or status code
    }
};
于 2018-04-29T12:02:22.123 回答