Files
community-rule/scripts/prune-sessions.ts
T
adilallo 46c34a6323 Delete unused login and invite tokens on a daily job, and keep their URLs out of logs.
Expired sign-in, save-progress, email-change, and invitation tokens (and any draft they carry) are cleared by a Cloudron scheduler task. Page timing logs record the path without the query string, and mail failures log a request id instead of an address.
2026-11-10 15:34:33 -07:00

68 lines
1.7 KiB
TypeScript

/**
* Prune expired sessions, unused login/save-progress/email-change tokens
* (and any draft they carry), unused invitation tokens, and old workshop
* inquiries.
*
* Usage (from repo root):
* node --env-file=.env --import tsx scripts/prune-sessions.ts # global sweep
* node --env-file=.env --import tsx scripts/prune-sessions.ts <userId> # sessions for one user
*
* Intentionally does NOT import `lib/server/session` — that module pulls in
* `next/headers` which requires a Next request context. For ad-hoc DB
* surgery we talk to Prisma directly.
*/
import { PrismaClient } from "@prisma/client";
import { pruneExpiredRecords } from "../lib/server/pruneExpiredRecords";
const prisma = new PrismaClient();
async function main() {
const userId = process.argv[2];
const now = new Date();
if (userId) {
const before = await prisma.session.count({ where: { userId } });
const { count } = await prisma.session.deleteMany({
where: {
expiresAt: { lt: now },
userId,
},
});
const after = await prisma.session.count({ where: { userId } });
console.log(
JSON.stringify(
{
scope: userId,
now: now.toISOString(),
sessionsBefore: before,
pruned: count,
sessionsAfter: after,
},
null,
2,
),
);
return;
}
const result = await pruneExpiredRecords(prisma, now);
console.log(
JSON.stringify(
{
scope: "global",
now: now.toISOString(),
...result,
},
null,
2,
),
);
}
main()
.catch((err) => {
console.error(err);
process.exitCode = 1;
})
.finally(() => prisma.$disconnect());