Repair an Express Authentication Endpoint Without Blocking the Event Loop
OktaBackend Engineer · OnsiteLast updated
Repair an Express authentication endpoint with input validation, asynchronous bcrypt, safe errors, correct HTTP status codes, and event-loop-aware resource controls.
Review and repair a Node.js Express /authenticate endpoint. It accepts a username and password, queries a database for the user, compares the supplied password with a stored bcrypt hash, and returns success or an error.
The existing implementation lacks input validation and error handling and uses synchronous bcrypt comparison. Explain a corrected implementation with appropriate HTTP status codes and asynchronous password comparison so password hashing does not block the event loop.
Constraints & Assumptions
- The original code and database library are not supplied. Describe any repository interface you introduce as an illustrative adapter.
- Practice clarification: the endpoint returns JSON success for valid credentials. Session or token issuance is a separate integration step and must occur only after successful verification.
- Do not log passwords or expose database errors and password hashes to the client.
Clarifying Questions to Ask
- What input formats and length limits does the application support, and are usernames normalized during account creation?
- Should malformed input be distinguished from a well-formed but invalid credential pair?
- Which component creates the session or token after credential verification, and what is the existing login-throttling policy?
Hints
Hint 1
Follow every asynchronous failure path: Database lookup and password comparison can fail independently. Ensure both failures reach a handler that ends the request without exposing internals.
Follow-up questions
- Why can asynchronous bcrypt still become a bottleneck under many concurrent login attempts?
- How would you reduce differences between the nonexistent-user and wrong-password paths?
- Which failures should return a client error, and which should return a server error?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.