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.
This commit is contained in:
adilallo committed 2026-11-10 15:34:33 -07:00
1 parent 73d0856004
commit 46c34a6323
28 files changed
+636 -45

No files matched your search

+34 -14
View File
@@ -1,37 +1,57 @@
/**
* Manual prune-expired-sessions script for CR-85 verification.
* 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> # per-user
* 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();
const before = await prisma.session.count();
const { count } = await prisma.session.deleteMany({
where: {
expiresAt: { lt: now },
...(userId ? { userId } : {}),
},
});
const after = await prisma.session.count();
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: userId ?? "global",
scope: "global",
now: now.toISOString(),
sessionsBefore: before,
pruned: count,
sessionsAfter: after,
...result,
},
null,
2,