Repair an Express authentication endpoint with input validation, asynchronous bcrypt, safe errors, correct HTTP status codes, and event-loop-aware resource controls.
Repair an Express Authentication Endpoint Without Blocking the Event Loop
Company: Okta
Role: Backend Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
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?
### What a Strong Answer Covers
- Type and length validation before querying or hashing, with a bounded request body.
- Parameterized database lookup and asynchronous bcrypt comparison.
- Generic credential-failure responses, safe operational-error handling, and a single response per request.
- The distinction between avoiding event-loop blocking and controlling the resource cost of many concurrent bcrypt operations.
```hint 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?
Overview: 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 Guidance
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?
What a Strong Answer Covers Guidance
Type and length validation before querying or hashing, with a bounded request body.
Parameterized database lookup and asynchronous bcrypt comparison.
Generic credential-failure responses, safe operational-error handling, and a single response per request.
The distinction between avoiding event-loop blocking and controlling the resource cost of many concurrent bcrypt operations.
Follow-up Questions Guidance
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?