diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 3914a18..48840c0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -106,7 +106,6 @@ model AuthSession { id String @id @unique @default(uuid()) @db.Uuid userId BigInt token String @unique - expiresAt DateTime ipAddress String? userAgent String? browser String? @@ -114,6 +113,7 @@ model AuthSession { device String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt + expiresAt DateTime user user @relation(fields: [userId], references: [userid]) } diff --git a/utils/cronJobs.ts b/utils/cronJobs.ts index fe69497..4b18316 100644 --- a/utils/cronJobs.ts +++ b/utils/cronJobs.ts @@ -5,6 +5,9 @@ import { runRoleSyncCron } from "@/utils/crons/update-roles"; import { runBirthdayCron } from "@/utils/crons/birthday"; import { runActivityReset } from "@/utils/crons/resetActivity"; import { runMilestoneCron } from "@/utils/crons/milestones"; +import { runSessionCron } from "./crons/authSessions"; +import { runOAuthCron } from "./crons/authState"; +import { runPendingVerificationCron } from "./crons/pVerification"; let initialized = false; @@ -53,6 +56,30 @@ export async function initCronJobs() { } }); + cron.schedule("*/30 * * * *", async () => { + try { + await runSessionCron(); + } catch (err) { + console.error("[CRON][AUTH]", err); + } + }); + + cron.schedule("*/5 * * * *", async () => { + try { + await runOAuthCron(); + } catch (err) { + console.error("[CRON][OAUTH]", err); + } + }); + + cron.schedule("*/5 * * * *", async () => { + try { + await runPendingVerificationCron(); + } catch (err) { + console.error("[CRON][OAUTH]", err); + } + }); + console.log("[STARTUP] All crons scheduled."); } catch (err) { console.error( diff --git a/utils/crons/authSessions.ts b/utils/crons/authSessions.ts new file mode 100644 index 0000000..fb751d3 --- /dev/null +++ b/utils/crons/authSessions.ts @@ -0,0 +1,16 @@ +import prisma from "@/utils/database"; + +export async function runSessionCron() { + const result = await prisma.authSession.deleteMany({ + where: { + expiresAt: { + lte: new Date(), + }, + }, + }); + + return { + success: true, + deleted: result.count, + }; +} \ No newline at end of file diff --git a/utils/crons/authState.ts b/utils/crons/authState.ts new file mode 100644 index 0000000..61d8c9b --- /dev/null +++ b/utils/crons/authState.ts @@ -0,0 +1,16 @@ +import prisma from "@/utils/database"; + +export async function runOAuthCron() { + const result = await prisma.oAuthState.deleteMany({ + where: { + expiresAt: { + lte: new Date(), + }, + }, + }); + + return { + success: true, + deleted: result.count, + }; +} \ No newline at end of file diff --git a/utils/crons/pVerification.ts b/utils/crons/pVerification.ts new file mode 100644 index 0000000..3e56318 --- /dev/null +++ b/utils/crons/pVerification.ts @@ -0,0 +1,16 @@ +import prisma from "@/utils/database"; + +export async function runPendingVerificationCron() { + const result = await prisma.pendingVerification.deleteMany({ + where: { + expiresAt: { + lte: new Date(), + }, + }, + }); + + return { + success: true, + deleted: result.count, + }; +} \ No newline at end of file