Skip to content

fix(auth): prevent login and signup when user is already authenticated - #315

Merged
omegascorp merged 3 commits into
modelence:mainfrom
ankit-0903:fix/prevent-login-signup-when-authenticated
Jul 27, 2026
Merged

omegascorp merged 3 commits into
modelence:mainfrom
ankit-0903:fix/prevent-login-signup-when-authenticated

Conversation

@ankit-0903

@ankit-0903 ankit-0903 commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

What & Why

Currently, handleLoginWithPassword and handleSignupWithPassword do not reject requests from users who are already logged in until after performing IP rate limiting, argument validation, and disposable email checks.

This PR moves the authentication check (if (user)) to the very top of both try blocks to throw an AuthError('User is already authenticated', 'ALREADY_AUTHENTICATED') early. This prevents state overlaps, avoids duplicate rate-limit consumption, and skips unnecessary async checks.

Closes #311

How it was tested

  1. Added unit tests in src/auth/login.test.ts and src/auth/signup.test.ts verifying that an AuthError (ALREADY_AUTHENTICATED) is thrown when user is in context and asserting that rate-limiting and disposable email checks are not called.
  2. Verified npm run build succeeds.
  3. Verified all 779 unit tests pass in serial mode (npx vitest run --fileParallelism=false).

Note

Low Risk
Small, early-return guard in auth handlers with tests; no change to successful login/signup paths.

Overview
Password login and signup now fail fast when the request context already has an authenticated user, instead of running rate limits and other checks first.

Both handleLoginWithPassword and handleSignupWithPassword throw a ValidationError with message User is already authenticated and code ALREADY_AUTHENTICATED immediately after session validation (login) or at the start of the handler (signup). That avoids consuming sign-in/sign-up rate limits and skips disposable-email and DB lookups on signup when someone is already logged in.

ValidationError now accepts an optional code in its constructor (aligned with AuthError), so clients can branch on ALREADY_AUTHENTICATED without string-matching messages.

Unit tests assert the error shape and that rate limiting (and on signup, disposable checks and findOne) are not invoked when user is present.

Reviewed by Cursor Bugbot for commit 3f2d1a4. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6176979. Configure here.

Comment thread packages/modelence/src/auth/login.ts Resolved
@omegascorp

Copy link
Copy Markdown
Collaborator

@ankit-0903 looks good. Please resolve the conflicts

@ankit-0903

ankit-0903 commented Jul 26, 2026 via email

Copy link
Copy Markdown
Contributor Author

@omegascorp
omegascorp merged commit 0e072e4 into modelence:main Jul 27, 2026
3 checks passed
@ankit-0903

ankit-0903 commented Jul 27, 2026 via email

Copy link
Copy Markdown
Contributor Author

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.

Bug: login and signup handlers do not reject already authenticated users

2 participants