fix: add error handling to authentication controllers#56
Conversation
|
@om-dev007 is attempting to deploy a commit to the rishabhjtripathi2903-3434's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthrough
ChangesAuth Controller Error Handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/src/controllers/authController.ts`:
- Around line 64-67: The catch blocks in authController.ts are exposing raw
error messages to clients, which leaks internal implementation details and
violates security practices. In both the catch block around lines 64-67 and the
one around lines 103-105, replace the dynamic error.message with a fixed generic
message like "Internal server error" in the res.status(500).json() response.
Keep the detailed error information (error.message and full error object) only
in server-side logging using a logger or console.error so debugging information
is available internally but not exposed to clients.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 049a1f9a-7534-4ace-90f3-9eabb34ea26f
📒 Files selected for processing (1)
backend/src/controllers/authController.ts
| res.status(500).json({ | ||
| success: false, | ||
| message: error.message ||'Internal server error' | ||
| }); |
There was a problem hiding this comment.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Do not return raw exception messages in auth 500 responses (Line 66, Line 105).
Both catch blocks currently expose error.message to clients, which can leak internal implementation details and conflicts with the PR’s “no internal error details” requirement. Return a fixed generic message to clients and keep detailed error info only in server logs.
Suggested patch
- res.status(500).json({
- success: false,
- message: error.message ||'Internal server error'
- });
+ res.status(500).json({
+ success: false,
+ message: 'Internal server error'
+ });
@@
- res.status(500).json({
- success: false,
- message: error.message || 'Internal server error'
- });
+ res.status(500).json({
+ success: false,
+ message: 'Internal server error'
+ });Also applies to: 103-105
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@backend/src/controllers/authController.ts` around lines 64 - 67, The catch
blocks in authController.ts are exposing raw error messages to clients, which
leaks internal implementation details and violates security practices. In both
the catch block around lines 64-67 and the one around lines 103-105, replace the
dynamic error.message with a fixed generic message like "Internal server error"
in the res.status(500).json() response. Keep the detailed error information
(error.message and full error object) only in server-side logging using a logger
or console.error so debugging information is available internally but not
exposed to clients.
Summary
This PR adds proper error handling to authentication controllers by wrapping asynchronous operations in try-catch blocks.
Changes Made
Added try-catch blocks to registerUser
Added try-catch blocks to loginUser
Improved error handling for authentication-related operations
Added server error responses for unexpected failures
Added error logging for easier debugging
Why
Authentication controllers perform multiple asynchronous operations such as database queries and password hashing. Without proper error handling, unexpected failures can lead to unhandled exceptions and inconsistent API responses.
Issue
Closes #53
Summary by CodeRabbit