feat: implement automated refresh token cleanup and schema optimization#583
feat: implement automated refresh token cleanup and schema optimization#583udaycodespace wants to merge 4 commits into
Conversation
|
@udaycodespace is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @udaycodespace, Thanks for opening this pull request. This PR has been automatically classified based on the files modified. Applied Labels
Primary Review Area
Reviewer@Harxhit has been identified as the primary reviewer for this pull request. If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer. Thank you for your contribution! |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
|
Please attach the video proof as mentioned in the issue. |
|
Hi @Harxhit, As requested, I've attached the demo video for Issue #543.
Video: The video covers:
Please let me know if you'd like me to record a follow-up video with audio or additional demonstrations. Thank you. |
Harxhit
left a comment
There was a problem hiding this comment.
Checked this out locally and tested it. Nice, focused feature — the active-token-safety reasoning in the description holds up.
Verified ✅
npm run typecheck→ passesnpx vitest run src/__tests__/refreshTokenCleanup.test.ts→ 16/16 pass (theConnection failure/Transaction deadlocklogs are the intentional error-path tests with a mocked Prisma — expected)eslinton all changed files → clean- Logic is correct:
deleteManyonrevokedAt != null OR expiresAt < nowcannot match active tokens (revokedAt == null AND expiresAt > now). - Plugin is registered after
prismaPlugin, guarded byNODE_ENV !== 'test', and clears its interval inonClose— good lifecycle hygiene.
Suggestions
1. (Medium) No migration for the new indexes. schema.prisma adds @@index([expiresAt]) and @@index([revokedAt]), but there's no prisma/migrations/** file in the PR. Without a migration, prisma migrate deploy won't create these indexes on existing/production databases, so the cleanup query won't actually benefit from them there. Consider:
npx prisma migrate dev --name refresh_token_cleanup_indexesand committing the generated migration.
2. (Nit) app.ts was reformatted to double quotes. The rest of the codebase — and this PR's own new files — use single quotes. The import-block reformatting adds diff churn unrelated to the feature and will muddy git blame. Suggest reverting app.ts to single quotes to keep the diff minimal.
3. (Minor) Startup cleanup blocks boot. await runCleanup() runs before the server starts listening. It's wrapped in try/catch so a failure won't crash startup, but a slow DB will delay readiness. Optional: let the first run be non-blocking (void runCleanup()), since the interval runs it anyway.
4. (Minor / future) Multi-instance deployments will each run their own setInterval, duplicating the cleanup. deleteMany is idempotent so it's safe — just redundant work. Worth a note if the service scales horizontally (a single scheduled job or an advisory lock would avoid it).
Overall: clean, well-tested, and correct. The missing migration (#1) is the only thing I'd want addressed before merge; the rest are optional.
Reviewed locally on commit head of feat/543-refresh-token-cleanup.
|
Thanks for the review and for testing it locally. My planned follow-up:
I'll work on these updates and push them by this afternoon. Thank you for the detailed review. |
The video should have DB entries getting deleted. |
Thanks for the clarification. Understood. The current video demonstrates the implementation, tests, and verification steps, but I understand that you'd like to see the actual database records being removed by the cleanup process. I'll prepare a database-backed demo showing the refresh token records before cleanup and after cleanup execution, and I'll upload the updated video by tomorrow afternoon. Thank you for the clarification. |
|
PR #583 Status Update Progress: ~80-85% Completed
Remaining |
|
@Harxhit , Migration Question (#1)I'm a bit stuck on the migration part and wanted to check before proceeding. I looked into it and found that the repository currently only has these committed migrations:
However, the current Because of this, when I generate a migration locally, Prisma creates a much larger migration that tries to create several existing schema models/tables instead of only adding the new indexes for Could you please let me know the preferred approach here? I want to avoid committing a large unrelated migration just to add these indexes. Remaining
|
|
Hi @HARSHIT Singh, All review feedback for PR #583 has now been addressed.
Demo Video: https://drive.google.com/file/d/1Oybd7eT1OyKVUvXuXTA969BI9l-dScyJ/view?usp=sharing Video Summary
Terminal EvidenceFinal Result
Ready for re-review. Thank you. |
Summary
Implements automated refresh token cleanup for expired and revoked refresh tokens.
This PR adds automated cleanup of stale refresh tokens, database indexes to support cleanup queries, scheduler integration, and test coverage to verify cleanup behavior while ensuring active tokens remain untouched.
Closes #543
Type of Change
What Changed
RefreshToken.expiresAtandRefreshToken.revokedAtrefreshTokenCleanupService.tsfor expired/revoked token cleanuprefreshTokenCleanup.tsscheduled cleanup pluginonCloseREFRESH_TOKEN_CLEANUP_INTERVAL_MSto.env.exampleHow to Test
Type Check
Run Cleanup Tests
Verify
Checklist
Validation
Screenshots / Recordings
Files changed
Test execution (16/16 passing)
TypeScript validation
Commit and repository status
Additional Context
Active Token Safety
The cleanup query only targets:
OR
Active refresh tokens satisfy:
AND
Therefore active refresh tokens cannot match the cleanup query and are not deleted.
Demo Video
The issue requests a demo video showing cleanup behavior.
For now, I have attached validation screenshots showing the implementation, test execution, type checking, and repository status.
If review feedback results in additional changes, I will address those first and then record a final demo video covering the complete cleanup workflow to avoid re-recording multiple times.