我试图阻止用户在3次尝试失败后登录,因为他们提供了正确的用户名或电子邮件和错误的密码。
Accounts.validateLoginAttempt(function(info){
var user = info.user;
var failAttemp = user.profile.loginFaileAttempt;
if(failAttemp == 3){
console.log('you need to contact the admin!')
return false;
}else{
if(Meteor.Error == 'Incorrect password '){
// incremnt the fail attempts
failAttemp++;
console.log(failAttemp);
}
}
return true;
// success login set to 0
failAttemp = 0;
});
但它不起作用,我做错了什么,有流星的方法吗?
谢谢。
失败的尝试计数不会在代码的users集合中更新。最后一行失败Attemp=0;将永远不会执行,因为函数已经返回。
此外,我看到了一些您可能想要解决的问题:Meteor.Error不是检查输入的密码是否错误的正确方法。它将是未定义的,甚至由于"错误密码"中的额外空间而不会触发。使用info
参数附带的error
对象,并使用错误代码而不是消息。
未注册用户的登录尝试无论如何都会传递给Accounts.validateLoginAttempt
。在这种尝试中,info.user
将为空。除此之外,最好检查用户对象是否存在profile
字段。
当用户有3次尝试失败并且第4次尝试时,他不会被告知出了什么问题。他仍然看到"密码不正确",在服务器的控制台上显示"你需要联系管理员!"。你可以抛出一个Meteor.Error,并显示更多信息。
当用户有3次尝试失败时,他将处于"禁用"状态。我的意思是,即使他记得自己的正确密码,他也不能再登录了。首先检查尝试是否被禁止,然后检查失败的尝试次数。
当用户在尝试失败后输入正确的密码时,尝试失败的次数应该返回到0,至少我认为这是你想要查看代码的内容(最后一行无法访问的代码)。
以下是一个解决方案的示例:
- 保存失败的尝试
- 根据错误代码而不是消息执行检查
- 在3次尝试失败后显示信息
- 正确处理未注册的用户
- 允许用户在尝试失败后登录(如果他们记得自己的密码)
- 成功登录后重置失败尝试计数
代码:
Accounts.validateLoginAttempt(function(info){
var user = info.user;
if (!user)
return false;
var failAttempt = 0;
if (user.profile)
failAttempt = user.profile.loginFaileAttempt;
var loginAllowed = false;
if(info.error && info.error.error == 403){
if(failAttempt >= 3) {
console.log('you need to contact the admin!');
throw new Meteor.Error(403, 'you need to contact the admin!');
}
// increment the fail attempts
failAttempt++;
console.log(failAttempt);
loginAllowed = false;
} else {
// success login set to 0
failAttempt = 0;
loginAllowed = true;
}
Meteor.users.update({_id: user._id}, {$set: {'profile.loginFaileAttempt': failAttempt}});
return loginAllowed;
});