Skip to content

Missing Error Handling in checkAuth Middleware #16

@beetle-ai

Description

@beetle-ai

ISSUE_NUMBER: GH-6

Description

The checkAuth middleware in src/middleware/checkAuth.ts lacks proper error handling for JWT verification and user lookup.
File: repositories/chatgptapi/src/middleware/checkAuth.ts
Line: 40
Severity: high

Current Behavior

The jwt.verify function and User.findById can throw unhandled exceptions if the token is invalid or the user does not exist.

Expected Behavior

The middleware should gracefully handle errors and return appropriate error responses.

Suggested Fix

Add try-catch blocks to handle JWT verification errors and user lookup errors.

Code Context

const decoded = jwt.verify(token, secret) as JwtPayload;
req.user = await User.findById(decoded.id);

Additional Notes

This affects application stability and could expose sensitive information.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions