add:module:auth:*logout | add logout module and clean all session in system

This commit is contained in:
2025-06-19 17:16:54 +07:00
parent fdfafcd2e0
commit ac82676505
15 changed files with 130 additions and 5 deletions

View File

@ -0,0 +1,16 @@
import { AppError } from "../../../helpers/error/instances/app";
import { redis } from "../../../utils/databases/redis/connection";
export const deleteUserSessionFromCacheRepo = async (
userId: string,
sessionId: string
) => {
try {
const deleteUserSessionFromCache = redis.del(
`${process.env.APP_NAME}:users:${userId}:sessions:${sessionId}`
);
return deleteUserSessionFromCache;
} catch (error) {
throw new AppError(500, "Error while remove data from cache", error);
}
};

View File

@ -0,0 +1,19 @@
import { AppError } from "../../../helpers/error/instances/app";
import { prisma } from "../../../utils/databases/prisma/connection";
export const deleteUserSessionFromDBRepo = async (sessionId: string) => {
try {
const deleteUserSessionFromCacheDB = await prisma.userSession.update({
where: {
id: sessionId,
},
data: {
deletedAt: new Date(),
},
});
return deleteUserSessionFromCacheDB;
} catch (error) {
throw new AppError(500, "Error while remove delete from database", error);
}
};

View File

@ -19,7 +19,6 @@ export const findUniqueUserSessionInDBRepo = async (identifier: string) => {
},
},
omit: {
deletedAt: true,
updatedAt: true,
},
});

View File

@ -20,7 +20,6 @@ export const createUserSessionRepo = async (
},
omit: {
lastOnline: true,
deletedAt: true,
createdAt: true,
updatedAt: true,
},

View File

@ -8,7 +8,7 @@ export const storeUserSessionToCacheRepo = async (
) => {
try {
await redis.set(
`${process.env.app_name}:users:${userSession.userId}:sessions:${userSession.id}`,
`${process.env.APP_NAME}:users:${userSession.userId}:sessions:${userSession.id}`,
String(userSession.validUntil),
"EX",
timeExpires