fix(adapter): handle delete gracefully

This commit is contained in:
Bereket Engida
2024-10-21 10:07:19 +03:00
parent 7a9002f56b
commit a37532bcf9
3 changed files with 24 additions and 10 deletions

View File

@@ -164,7 +164,9 @@ export const prismaAdapter = (
const { model, where } = data;
const whereClause = whereConvertor(where);
return await db[model].delete({ where: whereClause });
return await db[model].delete({ where: whereClause }).catch((e) => {
//handle delete gracefully (if not found)
});
},
async deleteMany(data) {
const { model, where } = data;

View File

@@ -293,6 +293,18 @@ export async function runAdapterTest(opts: AdapterTestOptions) {
expect(findRes.length).toBe(0);
});
test("shouldn't throw on delete record not found", async () => {
await adapter.delete({
model: "user",
where: [
{
field: "id",
value: "5",
},
],
});
});
test("shouldn't throw on record not found", async () => {
const res = await adapter.findOne({
model: "user",

View File

@@ -119,7 +119,7 @@ export const multiSession = (options?: MultiSessionConfig) => {
return ctx.json(session);
},
),
DeviceSession: createAuthEndpoint(
revokeDeviceSession: createAuthEndpoint(
"/multi-session/revoke",
{
method: "POST",
@@ -143,7 +143,7 @@ export const multiSession = (options?: MultiSessionConfig) => {
}
const session =
await ctx.context.internalAdapter.findSession(sessionId);
if (!session || session.session.expiresAt < new Date()) {
if (!session) {
ctx.setCookie(multiSessionCookieName, "", {
...ctx.context.authCookies.sessionToken.options,
maxAge: 0,
@@ -228,13 +228,13 @@ export const multiSession = (options?: MultiSessionConfig) => {
Object.entries(cookies).map(async ([key, value]) => {
if (isMultiSessionCookie(key)) {
ctx.setCookie(key, "", { maxAge: 0 });
try {
await ctx.context.internalAdapter.deleteSession(
key.split("_multi-")[1],
);
} catch (e) {
// ignore
}
const id = key.split("_multi-")[1];
/**
* Adapter fails if deleting a session that doesn't exist
*/
await ctx.context.internalAdapter
.deleteSession(id)
.catch((e) => {});
}
}),
);