fix: multi-session listing removing cookies before they are initatied

This commit is contained in:
Bereket Engida
2024-11-15 22:31:28 +03:00
parent 49592de059
commit dc1b0eee8a
2 changed files with 24 additions and 21 deletions
@@ -230,9 +230,13 @@ export const prismaAdapter =
async delete(data) {
const { model, where } = data;
const whereClause = convertWhereClause(model, where);
await db[getModelName(model)].delete({
where: whereClause,
});
try {
await db[getModelName(model)].delete({
where: whereClause,
});
} catch (e) {
// If the record doesn't exist, we don't want to throw an error
}
},
async deleteMany(data) {
const { model, where } = data;
@@ -54,25 +54,24 @@ export const multiSession = (options?: MultiSessionConfig) => {
const sessions =
await ctx.context.internalAdapter.findSessions(sessionIds);
const validSessions = sessions
.filter(
(session) => session && session.session.expiresAt > new Date(),
)
.filter(
(session, index, self) =>
index === self.findIndex((s) => s.user.id === session.user.id),
);
const validSessions = sessions.filter(
(session) => session && session.session.expiresAt > new Date(),
);
Object.entries(cookies)
.filter(([key]) => isMultiSessionCookie(key))
.forEach(([key, value]) => {
if (!validSessions.some((s) => s.session.id === value)) {
ctx.setCookie(key, "", {
...ctx.context.authCookies.sessionToken.options,
maxAge: 0,
});
}
});
for (const [key] of Object.entries(cookies).filter(([key]) =>
isMultiSessionCookie(key),
)) {
const sessionId = await ctx.getSignedCookie(
key,
ctx.context.secret,
);
if (!validSessions.some((s) => s.session.id === sessionId)) {
ctx.setCookie(key, "", {
...ctx.context.authCookies.sessionToken.options,
maxAge: 0,
});
}
}
return ctx.json(validSessions);
},