我正在开发一个使用护照身份验证进行登录和注册过程的SPA。问题是一切正常,我甚至可以显示成功通知,但我无法找到一种方法来显示错误通知,以防用户执行错误操作。
这是我在passport-init中注册的代码:
passport.use('signup', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done){
var email = req.body.email;
User.findOne({$or:
[
{username: username},
{email: email}
]
}, function(err, user){
if(err){
return done(err, false);
}
if(user) {
console.log('username or email already taken '+req.body.username);
return done(null, false, {message: 'Username or email already taken.'});
}
user = new User();
if(req.body.password===req.body.username || req.body.password===req.body.fullname
|| req.body.password===req.body.email){
console.log('The password can not be equals to your fullname'+
', username or email. Try another password.');
return done(null, false, {message: 'The password can not be equals to your fullname'+
', username or email. Try another password.'});
}
然后,如果不发生这种情况,则该方法不会返回用户数据的错误。
以下是将结果传递给angular的路径代码:
router.get('/failure', function(req, res){
res.send({state: 'failure', user: null, message: "Invalid username or password"});
});
router.post('/signup', passport.authenticate('signup',{
successRedirect: '/auth/success',
failureRedirect: '/auth/failure',
failureFlash: true
}));
最后我得到这个角度以便打电话并得到答复:
$scope.register = function(){
$http.post('/auth/signup', $scope.user).then(
function(data){
$rootScope.authenticated = true;
$rootScope.current_user = data.user.username;
$rootScope.signUp = true;
},
function(data, status){
console.log(status);
}
);
};
就是这样。当然,角度代码在控制器内部。我刚开始学习构建MEAN堆栈应用程序,所以任何帮助都将受到赞赏。
答案 0 :(得分:2)
如果有自定义错误/自定义逻辑,您应该将错误发送回客户端:
setTimeout
与您的代码中的其他条件相同。