Skip to content

fix: return 401 for expired or invalid tokens in authMiddleware - #216

Merged
TheCodeHeist-Coder merged 1 commit into
TheCodeHeist-Coder:mainfrom
Anshuu2004:fix/auth-invalid-token-401
Oct 26, 2026
Merged

TheCodeHeist-Coder merged 1 commit into
TheCodeHeist-Coder:mainfrom
Anshuu2004:fix/auth-invalid-token-401

Conversation

@Anshuu2004

Copy link
Copy Markdown
Contributor

What does this PR do?

authMiddleware was answering every jwt.verify failure with a 500, so an expired or garbage token never gave the client a 401 to log the user out on. Now expired, malformed and badly signed tokens, and a token whose payload has no userId, get a 401. Any other error still returns 500. The console.log stack trace no longer fires for those 401 cases.

http-server had no test setup, so I added vitest the same way ws-server and streaming have it (test script, tests excluded from the tsc build) and a "Test http-server" step in ci.yml. The new test file calls the middleware directly with a fake req/res and real signed tokens, so it needs no DB or services. It covers a valid token, expired token, garbage token, wrong secret, payload without userId, and a non-token error still giving 500.

Related issue

Closes #211

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactoring
  • Performance
  • Tests
  • Build / CI / deployment

Which services does this touch?

  • apps/frontend
  • apps/http-server
  • apps/ws-server
  • apps/genAI
  • packages/db (schema or Prisma changes)
  • Docker / CI

How did you test this?

  • pnpm --filter http-server run test: 6 tests pass.
  • Ran the same tests against the old middleware: the expired token, garbage token, wrong secret and no userId tests fail (they get 500), the valid token and non-token error tests pass.
  • pnpm run check-types passes.
  • pnpm --filter http-server run build (tsc -b) passes, and the test file is not emitted into dist.
  • pnpm install --frozen-lockfile works with the lockfile change (only the vitest entry for http-server was added by hand to pnpm-lock.yaml, to avoid pnpm rewriting unrelated entries).

I did not run the app with docker-compose or hit a real endpoint with an expired token, only the unit tests above. I also did not run the full pnpm run build, only the http-server build. pnpm run lint fails for me in the frontend with the existing eslint errors (CI has continue-on-error for it), and http-server has no lint script, so nothing in this change got linted.

Checklist

  • I ran pnpm run lint
  • I ran pnpm run check-types
  • I ran pnpm run build
  • The app still starts with docker-compose up
  • I updated documentation if this changes behaviour or setup
  • I did not commit any .env file, API key or other secret

First pull request here? CONTRIBUTING.md walks through the
whole process, and a checkbox left unticked is not a problem — say so and we
will help.

jwt.verify throws for expired, malformed and badly signed tokens, and the
catch answered all of them with 500, so the client never logged the user
out. Token errors (and a payload without userId) now get 401 and no stack
trace in the logs. Anything else still returns 500.

Adds a vitest setup to http-server with tests for the middleware, and a
CI step to run them.
@TheCodeHeist-Coder
TheCodeHeist-Coder merged commit f20540b into TheCodeHeist-Coder:main Oct 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An expired or invalid login token returns 500 instead of 401, so the app never logs the user out

2 participants