My code limits the user login attempts but when the page is refreshed it resets the user count.
login() {
/* Only attempt login if user has less than 5 login attempts */
if (this.failedAttempts < 4) {
this.auth.login(this.credentials).subscribe(() => {
this.router.navigateByUrl('/');
this.alert.success('Welcome! Thanks for logging in!');
}, (err) => {
this.failedAttempts++;
console.error(err);
this.alert.error('Login failed. Invalid email or password.');
});
/*If user reaches 5 failed attempts refresh number of failed attempts after 5 minutes and disable submit button*/
} else if (this.failedAttempts < 4) {
} else {
/*increments number of times locked out */
this.numLockedOut++;
this.alert.error('Login failed. Invalid email or password. Locked out for ' + (this.numLockedOut * 300000) / 60000 + ' minutes');
this.btnDisable = true;
setTimeout(() => this.failedAttempts = 0, this.numLockedOut * 300000);
setTimeout(() => this.btnDisable = false, this.numLockedOut * 300000);
}
}
How do I settimeout() without restarting the clock on page refresh?
You almost definitely want to be recording failed login attempts on your server, rather than in the browser. Even if you get this code working, it would be trivial for someone to edit the Javascript in their browser and make as many login attempts as they want.
If you did have your server recording attempted logins for a given username, then the response from the server could tell you that the number of login attempts has exceeded the allowed amount and the angular code could simply relay that information to the user. This would also solve your problem of the attempt count not persisting between page visits. :)