Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ model AuthSession {
id String @id @unique @default(uuid()) @db.Uuid
userId BigInt
token String @unique
expiresAt DateTime
ipAddress String?
userAgent String?
browser String?
os String?
device String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime
user user @relation(fields: [userId], references: [userid])
}

Expand Down
27 changes: 27 additions & 0 deletions utils/cronJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions utils/crons/authSessions.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
16 changes: 16 additions & 0 deletions utils/crons/authState.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
16 changes: 16 additions & 0 deletions utils/crons/pVerification.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
Loading