Skip to content

perf: optimize backend auth performance (#25)#28

Open
kanak-bansal wants to merge 1 commit into
imuniqueshiv:mainfrom
kanak-bansal:fix/auth-performance-optimization
Open

perf: optimize backend auth performance (#25)#28
kanak-bansal wants to merge 1 commit into
imuniqueshiv:mainfrom
kanak-bansal:fix/auth-performance-optimization

Conversation

@kanak-bansal

@kanak-bansal kanak-bansal commented Jul 3, 2026

Copy link
Copy Markdown

Closes #25

Summary

Optimized backend performance around authentication to reduce login/signup response time, without changing any auth logic, endpoints, or existing functionality.

Changes

  1. controllers/authControllers.js - Register no longer waits on the welcome email before responding. The response returns immediately after the user is created; the email sends in the background with error handling.

  2. config/nodeMailer.js - Enabled SMTP connection pooling (pool: true, maxConnections: 5) to avoid a fresh TCP/TLS handshake on every email send.

  3. server.js - Server now starts accepting requests (app.listen) before the vector store finishes initializing, instead of blocking startup on it. Vector store still initializes, just in the background afterward.

  4. middleware/userAuth.js - Removed debug console.log statements that ran on every authenticated request, and added .lean() to the user lookup for a lighter DB read.

Testing

  • Verified registration completes successfully and responds immediately (email still sends in background, confirmed via server logs).
  • Verified login works correctly.
  • Verified protected routes (dashboard) still load user data correctly after the .lean() change.
  • Confirmed no changes to auth endpoints, JWT logic, sessions, or any other functionality outside scope.

Summary by CodeRabbit

  • New Features
    • Email delivery is now handled more efficiently in the background, helping registration finish faster.
  • Bug Fixes
    • Improved login/session handling by accepting tokens from either cookies or standard authorization headers.
    • Removed unnecessary startup and authentication logging for cleaner app behavior.
  • Chores
    • Optimized email connection settings for better reliability and reduced connection overhead.
    • App startup now continues immediately while background services initialize asynchronously.

@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

@kanak-bansal is attempting to deploy a commit to the Shiv Raj Singh's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f1667ae-e1bd-4dec-8943-9b74be36b106

📥 Commits

Reviewing files that changed from the base of the PR and between be945c0 and 7c9fb02.

📒 Files selected for processing (4)
  • server/config/nodeMailer.js
  • server/controllers/authControllers.js
  • server/middleware/userAuth.js
  • server/server.js

📝 Walkthrough

Walkthrough

This PR optimizes backend authentication performance by enabling SMTP connection pooling, sending welcome emails asynchronously after the registration response, removing debug logging and adding .lean() to auth middleware queries, and making vector store initialization non-blocking during server startup.

Changes

Authentication and Startup Performance Changes

Layer / File(s) Summary
SMTP pooling and async welcome email
server/config/nodeMailer.js, server/controllers/authControllers.js
Nodemailer transporter enables pooling (pool: true, maxConnections: 5); registration response is returned before the welcome email is sent, with send failures caught and logged.
Auth middleware cleanup and lean queries
server/middleware/userAuth.js
Debug console logging around token extraction is removed, and .lean() is added to the authenticated user lookup for a plain-object result; default export unchanged.
Non-blocking vector store initialization
server/server.js
Vector store initialization is moved out of the blocking startup path into a non-blocking Promise chain run after the server starts listening, with logging on success/failure; SIGINT handler bracing adjusted.

Estimated code review effort: 2 (Simple) | ~15 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant AuthController
  participant Nodemailer
  participant Server
  participant VectorStore

  Client->>AuthController: register request
  AuthController->>Client: success response (immediate)
  AuthController->>Nodemailer: sendMail(welcome email, async)
  Nodemailer-->>AuthController: success/error (logged)

  Server->>Server: start listening
  Server->>VectorStore: initVectorStore() (async)
  VectorStore-->>Server: resolve/reject (logged)
Loading

Related issues: #25 (Optimize Backend Authentication Performance & Reduce API Response Time)

Suggested labels: performance, backend

Suggested reviewers: imuniqueshiv

Poem

A rabbit hops through server code,
No more waiting on the road,
Emails fly off on their own,
Vectors load while requests are shown,
Faster logins, cleaner trace,
Hop, hop, hooray — a quicker pace! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the backend auth performance optimization focus.
Linked Issues check ✅ Passed The changes target auth latency bottlenecks with email, query, and startup optimizations while preserving existing behavior.
Out of Scope Changes check ✅ Passed No clear out-of-scope code changes are present; the edits stay within backend performance and startup optimization scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
meetonmemory Ready Ready Preview, Comment Jul 3, 2026 5:19am

@imuniqueshiv imuniqueshiv left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kanak-bansal Thank you for your contribution and the performance improvements you've implemented. I appreciate the effort you've put into optimizing the authentication flow. 🚀

While reviewing the changes, I noticed one issue in the register() controller that needs to be fixed before this PR can be merged.

Registration Response Logic

The current implementation sends the response twice:

res.json(...);

return res.json(...);

This can result in:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Additionally, the asynchronous transporter.sendMail(...).catch(...) call is placed after the return statement, which makes it unreachable and therefore it will never execute.

Because of this:

  • The welcome email is still being sent synchronously (await transporter.sendMail(...)).
  • The intended performance optimization is not actually taking effect.

Please update the logic so that:

  • Only one response is returned.
  • The welcome email is sent asynchronously (without await).
  • No unreachable code remains.

Once this is fixed, I'll review the updated changes again.

Thanks again for your contribution!

@imuniqueshiv

Copy link
Copy Markdown
Owner

@kanak-bansal Any Updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚡ Optimize Backend Authentication Performance & Reduce API Response Time

2 participants