Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

chore(server): cleanup expired sessions (#7018)

forehalo29e7fa13711b
diff --git a/packages/backend/server/src/core/auth/service.ts b/packages/backend/server/src/core/auth/service.ts
index 69751bcee8..d2a6a73598 100644
--- a/packages/backend/server/src/core/auth/service.ts
+++ b/packages/backend/server/src/core/auth/service.ts
@@ -4,6 +4,7 @@ import {
NotAcceptableException,
OnApplicationBootstrap,
} from '@nestjs/common';
+import { Cron, CronExpression } from '@nestjs/schedule';
import type { User } from '@prisma/client';
import { PrismaClient } from '@prisma/client';
import type { CookieOptions, Request, Response } from 'express';
@@ -455,4 +456,23 @@ export class AuthService implements OnApplicationBootstrap {
to: email,
});
}
+
+ @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
+ async cleanExpiredSessions() {
+ await this.db.session.deleteMany({
+ where: {
+ expiresAt: {
+ lte: new Date(),
+ },
+ },
+ });
+
+ await this.db.userSession.deleteMany({
+ where: {
+ expiresAt: {
+ lte: new Date(),
+ },
+ },
+ });
+ }
}
diff --git a/packages/backend/server/src/core/auth/token.ts b/packages/backend/server/src/core/auth/token.ts
index 16c10d1204..0422621767 100644
--- a/packages/backend/server/src/core/auth/token.ts
+++ b/packages/backend/server/src/core/auth/token.ts
@@ -87,8 +87,8 @@ export class TokenService {
}
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
- cleanExpiredTokens() {
- return this.db.verificationToken.deleteMany({
+ async cleanExpiredTokens() {
+ await this.db.verificationToken.deleteMany({
where: {
expiresAt: {
lte: new Date(),