fix: prevent session loading hang from JWT callback DB failure

JWT callback was querying DB on every request which could hang if DB
is slow. Now only queries when token.id is missing, and wraps in
try/catch so session still resolves on DB errors.

v1.52.1

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
mARTin 2026-03-19 22:52:44 +01:00
parent 234bb25c1e
commit 3c17f6a89d
2 changed files with 13 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "my-weekly-todo-list",
"version": "1.52.0",
"version": "1.52.1",
"description": "A web-based weekly task management application that organizes to-dos and calendar events in a single, intuitive weekly view",
"main": "index.js",
"scripts": {

View File

@ -113,8 +113,9 @@ export const authOptions: NextAuthOptions = {
if (user) {
token.id = user.id;
}
// Refresh user ID from DB to handle stale sessions after DB changes
if (token.email) {
// Refresh user ID from DB periodically (not on every request)
if (token.email && !token.id) {
try {
const dbUser = await prisma.user.findUnique({
where: { email: token.email as string },
select: { id: true }
@ -122,6 +123,9 @@ export const authOptions: NextAuthOptions = {
if (dbUser) {
token.id = dbUser.id;
}
} catch (e) {
console.error("[AUTH] JWT callback DB lookup failed:", e);
}
}
return token;
},