From dd5c10bc7ec627ea56f828e15bbc8b271e67403b Mon Sep 17 00:00:00 2001 From: ksjitendra18 Date: Fri, 26 Jul 2024 11:17:00 +0530 Subject: [PATCH 1/2] fix: delete all sessions on password change --- src/data-access/sessions.ts | 4 ++++ src/use-cases/users.tsx | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/data-access/sessions.ts b/src/data-access/sessions.ts index a161d1e..1f81fff 100644 --- a/src/data-access/sessions.ts +++ b/src/data-access/sessions.ts @@ -6,3 +6,7 @@ import { eq } from "drizzle-orm"; export async function deleteSessionForUser(userId: UserId) { await db.delete(sessions).where(eq(sessions.userId, userId)); } + +export async function deleteAllSessions(userId: UserId, trx = db) { + await trx.delete(sessions).where(eq(sessions.userId, userId)); +} diff --git a/src/use-cases/users.tsx b/src/use-cases/users.tsx index ffb5a8f..27bc80b 100644 --- a/src/use-cases/users.tsx +++ b/src/use-cases/users.tsx @@ -38,6 +38,7 @@ import { } from "./errors"; import { db } from "@/db"; import { createTransaction } from "@/data-access/utils"; +import { deleteAllSessions } from "@/data-access/sessions"; export async function deleteUserUseCase( authenticatedUser: UserSession, @@ -152,6 +153,7 @@ export async function changePasswordUseCase(token: string, password: string) { await createTransaction(async (trx) => { await deletePasswordResetToken(token, trx); await updatePassword(userId, password, trx); + await deleteAllSessions(userId, trx); }); } From 06d03b1b27d4d1dcf03dc1a67a0fbeab4fdb8c06 Mon Sep 17 00:00:00 2001 From: ksjitendra18 Date: Sun, 28 Jul 2024 11:26:39 +0530 Subject: [PATCH 2/2] fix: remove redundant function and introduced db param in deleteSessionforUser --- src/data-access/sessions.ts | 6 +----- src/use-cases/users.tsx | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/data-access/sessions.ts b/src/data-access/sessions.ts index 1f81fff..a21d036 100644 --- a/src/data-access/sessions.ts +++ b/src/data-access/sessions.ts @@ -3,10 +3,6 @@ import { sessions } from "@/db/schema"; import { UserId } from "@/use-cases/types"; import { eq } from "drizzle-orm"; -export async function deleteSessionForUser(userId: UserId) { - await db.delete(sessions).where(eq(sessions.userId, userId)); -} - -export async function deleteAllSessions(userId: UserId, trx = db) { +export async function deleteSessionForUser(userId: UserId, trx = db) { await trx.delete(sessions).where(eq(sessions.userId, userId)); } diff --git a/src/use-cases/users.tsx b/src/use-cases/users.tsx index 27bc80b..c05e366 100644 --- a/src/use-cases/users.tsx +++ b/src/use-cases/users.tsx @@ -38,7 +38,7 @@ import { } from "./errors"; import { db } from "@/db"; import { createTransaction } from "@/data-access/utils"; -import { deleteAllSessions } from "@/data-access/sessions"; +import { deleteSessionForUser } from "@/data-access/sessions"; export async function deleteUserUseCase( authenticatedUser: UserSession, @@ -153,7 +153,7 @@ export async function changePasswordUseCase(token: string, password: string) { await createTransaction(async (trx) => { await deletePasswordResetToken(token, trx); await updatePassword(userId, password, trx); - await deleteAllSessions(userId, trx); + await deleteSessionForUser(userId, trx); }); }