✨ feat: add logout endpoint
Some checks failed
Integration Tests / integration-tests (pull_request) Failing after 51s
Some checks failed
Integration Tests / integration-tests (pull_request) Failing after 51s
This commit is contained in:
@ -1,13 +1,20 @@
|
||||
import { AppError } from "../../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../../helpers/error/instances/forwarder";
|
||||
import { jwtDecode } from "../../../../helpers/http/jwt/decode";
|
||||
import { redis } from "../../../../utils/databases/redis/connection";
|
||||
import { deleteUserSessionRepository } from "../../../userSession/repositories/deleteUserSession.repository";
|
||||
|
||||
export const logoutService = async (jwtToken?: any) => {
|
||||
try {
|
||||
if (!jwtToken) throw new AppError(403, "No auth token provided");
|
||||
|
||||
const jwtPayload = jwtDecode(jwtToken);
|
||||
return jwtPayload;
|
||||
|
||||
await redis.del(
|
||||
`${process.env.APP_NAME}:users:${jwtPayload.user.id}:sessions:${jwtPayload.id}`,
|
||||
);
|
||||
|
||||
return deleteUserSessionRepository(jwtPayload.id);
|
||||
} catch (error) {
|
||||
ErrorForwarder(error);
|
||||
}
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
|
||||
export const deleteUserSessionRepository = async (sessionId: string) => {
|
||||
try {
|
||||
return await prisma.userSession.update({
|
||||
where: {
|
||||
id: sessionId,
|
||||
},
|
||||
data: {
|
||||
isAuthenticated: false,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Failed to delete user session", error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user