Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7bdd183
feat: add scheduled refresh token and password reset cleanup job
GitAddRemote Apr 11, 2026
0507031
feat: add TokenCleanupService implementation
GitAddRemote Apr 11, 2026
c1c3647
fix: correct column name in cleanup queries, remove unused ConfigServ…
GitAddRemote Apr 13, 2026
8c2fc19
fix: register cleanup cron at runtime via SchedulerRegistry, add JEST…
GitAddRemote Apr 13, 2026
f50b7d0
fix: address PR #114 review comments on token cleanup job
GitAddRemote Apr 13, 2026
e90710a
fix: resolve merge conflicts with main and address PR #114 review items
GitAddRemote Apr 13, 2026
af8662b
fix: clarify partial index comments in token cleanup migration
GitAddRemote Apr 15, 2026
778a96b
chore: bump Dockerfile base image to node:20-slim
GitAddRemote Apr 16, 2026
af5077a
fix: harden TokenCleanupService and its spec
GitAddRemote Apr 16, 2026
1b3c7b4
chore: merge main into fix/ISSUE-98-token-cleanup-job
GitAddRemote Apr 16, 2026
58cb583
fix: clarify onApplicationBootstrap comment and deduplicate default cron
GitAddRemote Apr 16, 2026
9dedb81
fix: clarify migration index comments — partial indexes cover one OR …
GitAddRemote Apr 21, 2026
7ddea00
chore: merge main into fix/ISSUE-98-token-cleanup-job
GitAddRemote Apr 21, 2026
90d2df0
fix: register REFRESH_TOKEN_CLEANUP_CRON in env schema and migration …
GitAddRemote Apr 22, 2026
d00f19c
fix: replace as-any casts in token-cleanup spec with typed assertions
GitAddRemote Apr 22, 2026
095dce6
refactor: centralize default cleanup cron expression in shared constant
GitAddRemote Apr 22, 2026
5dba596
fix: log warning when REFRESH_TOKEN_CLEANUP_CRON is set but blank
GitAddRemote Apr 22, 2026
26c7745
test: mock CronJob in token-cleanup spec to prevent timer leaks
GitAddRemote Apr 22, 2026
a2cde75
fix: inline boolean constants in cleanup DELETEs and fix invalid-cron…
GitAddRemote Apr 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ REDIS_HOST=localhost
REDIS_PORT=6379
USE_REDIS_CACHE=true

# ─── Token Cleanup ──────────────────────────────────────────────────────────────
# Cron expression for the scheduled cleanup job (default: 3 AM daily).
REFRESH_TOKEN_CLEANUP_CRON="0 3 * * *"

# ─── Rate Limiting ──────────────────────────────────────────────────────────────
# TTL values are in milliseconds.
THROTTLE_TTL_MS=60000
Expand Down
2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:14
FROM node:20-slim

# Create app directory
WORKDIR /usr/src/app
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.2",
"cookie-parser": "^1.4.7",
"cron": "^4.3.3",
Comment thread
GitAddRemote marked this conversation as resolved.
"dotenv": "^16.4.5",
"figlet": "^1.8.0",
"helmet": "^8.0.0",
Expand Down
4 changes: 4 additions & 0 deletions backend/src/config/env.validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Joi from 'joi';
import { DEFAULT_CLEANUP_CRON } from '../modules/auth/token-cleanup.constants';

export const envValidationSchema = Joi.object({
// Application
Expand Down Expand Up @@ -55,4 +56,7 @@ export const envValidationSchema = Joi.object({
UEX_RATE_LIMIT_PAUSE_MS: Joi.number().default(2000),
UEX_ENDPOINTS_PAUSE_MS: Joi.number().default(2000),
UEX_API_KEY: Joi.string().allow('').default(''),

// Token cleanup scheduler (optional — defaults to 3 AM daily)
REFRESH_TOKEN_CLEANUP_CRON: Joi.string().default(DEFAULT_CLEANUP_CRON),
});
4 changes: 4 additions & 0 deletions backend/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { AddOrgInventorySummaryView1764950757207 } from './migrations/1764950757
import { SeedInventoryManagerRole1764961461064 } from './migrations/1764961461064-SeedInventoryManagerRole';
import { CreateOrgInventoryItemsTable1764964935270 } from './migrations/1764964935270-CreateOrgInventoryItemsTable';
import { AddUserInventoryUniqueIndex1765035000000 } from './migrations/1765035000000-AddUserInventoryUniqueIndex';
import { AddTokenCleanupIndexes1765038000000 } from './migrations/1765038000000-AddTokenCleanupIndexes';

export const AppDataSource = new DataSource({
type: 'postgres',
Expand Down Expand Up @@ -114,6 +115,9 @@ export const AppDataSource = new DataSource({
SeedInventoryManagerRole1764961461064,
CreateOrgInventoryItemsTable1764964935270,
AddUserInventoryUniqueIndex1765035000000,

// Token cleanup indexes (supports efficient revoked/expired deletes)
AddTokenCleanupIndexes1765038000000,
],
synchronize: false,
});
Expand Down
60 changes: 60 additions & 0 deletions backend/src/migrations/1765038000000-AddTokenCleanupIndexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

/**
* Adds indexes to support efficient token cleanup queries.
*
* The cleanup job deletes rows matching:
* refresh_tokens: revoked = true OR "expiresAt" < now
* password_resets: used = true OR "expiresAt" < now
*
* Partial indexes on the boolean flags keep index size small by only
* covering the rows that will actually be deleted.
*/
export class AddTokenCleanupIndexes1765038000000 implements MigrationInterface {
name = 'AddTokenCleanupIndexes1765038000000';

public async up(queryRunner: QueryRunner): Promise<void> {
// Partial index on revoked refresh tokens — optimizes the `revoked = true`
// predicate in the cleanup DELETE. Does not cover the expiresAt branch of
// the OR; that is handled by IDX_refresh_tokens_expiresAt below.
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS "IDX_refresh_tokens_revoked"
ON "refresh_tokens" ("revoked")
WHERE "revoked" = TRUE
`);

// Range index for expiry-based cleanup of refresh tokens
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS "IDX_refresh_tokens_expiresAt"
ON "refresh_tokens" ("expiresAt")
`);

// Partial index on used password resets — optimizes the `used = true`
// predicate in the cleanup DELETE. Does not cover the expiresAt branch of
// the OR; that is handled by IDX_password_resets_expiresAt below.
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS "IDX_password_resets_used"
ON "password_resets" ("used")
WHERE "used" = TRUE
`);

// Range index for expiry-based cleanup of password resets
await queryRunner.query(`
CREATE INDEX IF NOT EXISTS "IDX_password_resets_expiresAt"
ON "password_resets" ("expiresAt")
`);
}
Comment thread
GitAddRemote marked this conversation as resolved.

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX IF EXISTS "IDX_password_resets_expiresAt"`,
);
await queryRunner.query(`DROP INDEX IF EXISTS "IDX_password_resets_used"`);
await queryRunner.query(
`DROP INDEX IF EXISTS "IDX_refresh_tokens_expiresAt"`,
);
await queryRunner.query(
`DROP INDEX IF EXISTS "IDX_refresh_tokens_revoked"`,
);
}
}
3 changes: 2 additions & 1 deletion backend/src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JwtModule } from '@nestjs/jwt';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { TokenCleanupService } from './token-cleanup.service';
import { LocalStrategy } from './local.strategy';
import { JwtStrategy } from './jwt.strategy';
import { UsersModule } from '../users/users.module';
Expand All @@ -26,7 +27,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
}),
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, JwtStrategy],
providers: [AuthService, TokenCleanupService, LocalStrategy, JwtStrategy],
exports: [AuthService],
})
export class AuthModule {}
1 change: 1 addition & 0 deletions backend/src/modules/auth/token-cleanup.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_CLEANUP_CRON = '0 3 * * *';
Loading
Loading