mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-25 08:31:37 -05:00
fix(adapter): handle delete gracefully
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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) => {});
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user