Company
Date Published
Author
MongoDB
Word count
1644
Language
English
Hacker News points
None

Summary

Password Authentication with Mongoose (Part 2): Account Locking aims to prevent brute-force attacks against a site's login form by enforcing a maximum number of failed login attempts, thereby locking the account for a specified amount of time. The implementation involves adding two new properties to the User model: `loginAttempts` to track consecutive failures and `lockUntil` to store a timestamp indicating when the lock will expire. A faux-enum is also defined to represent reasons for failed login attempts. The login process is encapsulated within a single static method, `getAuthenticated`, which checks credentials against the stored hashed password and updates the `loginAttempts` and `lockUntil` fields accordingly. The method returns null along with an appropriate reason code if authentication fails or succeeds. To use this implementation, a user model is created with these new features, allowing for secure username/password authentication with account locking to prevent brute-force attacks.