{"record":{"id":"cb96af9533d82d60","repo":"nextauthjs/next-auth","slug":"updatesession-failed-to-fetch-updated-session","errorCode":null,"errorMessage":"[updateSession] Failed to fetch updated session","messagePattern":"\\[updateSession\\] Failed to fetch updated session","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/adapter-firebase/src/index.ts","lineNumber":217,"sourceCode":"                mapper.toDb(\"sessionToken\"),\n                \"==\",\n                partialSession.sessionToken\n              )\n              .limit(1)\n          )\n        ).docs[0]\n        if (!sessionSnapshot?.exists) return null\n\n        transaction.set(sessionSnapshot.ref, partialSession, { merge: true })\n\n        return sessionSnapshot.id\n      })\n\n      if (!sessionId) return null\n\n      const session = await getDoc(C.sessions.doc(sessionId))\n      if (session) return session\n      throw new Error(\"[updateSession] Failed to fetch updated session\")\n    },\n\n    async deleteSession(sessionToken) {\n      await deleteDocs(\n        C.sessions\n          .where(mapper.toDb(\"sessionToken\"), \"==\", sessionToken)\n          .limit(1)\n      )\n    },\n\n    async createVerificationToken(verificationToken) {\n      await C.verification_tokens.add(verificationToken)\n      return verificationToken\n    },\n\n    async useVerificationToken({ identifier, token }) {\n      const verificationTokenSnapshot = (\n        await C.verification_tokens","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/adapter-firebase/src/index.ts#L199-L235","documentation":"Firebase adapter's updateSession writes the updated session document with Firestore, then immediately re-reads it with getDoc to return the fresh record. If the re-read returns nothing, it concludes the update did not land and throws this error. It usually means the session document was deleted (or never matched the given sessionToken) between or during the update, or Firestore connectivity/permission issues caused a silent write/read mismatch.","triggerScenarios":"Calling updateSession with a sessionToken whose document no longer exists in Firestore (expired/deleted session), concurrent deleteSession racing updateSession, or Firestore rules/permissions causing the getDoc re-read to return an empty snapshot.","commonSituations":"Users logging out in one tab while another tab triggers a session refresh; clock-skew-driven expiry jobs deleting sessions; misconfigured Firestore security rules blocking reads for the adapter's service account; returning an already-deleted token from a stale client cookie.","solutions":["Verify the session document actually exists in Firestore for the given sessionToken before/while calling updateSession","Ensure deleteSession/expiry cleanup is not racing updateSession (single-tab logout, transactional deletes)","Check Firestore security rules and service-account permissions allow both write and read of the sessions collection","Clear the stale session cookie on the client and force a fresh sign-in flow","Check network/Firestore status if this occurs fleet-wide"],"exampleFix":"// before\nawait adapter.updateSession({ sessionToken: staleToken, expires: new Date() })\n// after\nconst existing = await adapter.getSessionAndUser(staleToken)\nif (existing) {\n  await adapter.updateSession({ sessionToken: staleToken, expires: new Date() })\n}","handlingStrategy":"try-catch","validationCode":"const snap = await getDoc(doc(db, 'sessions', sessionToken))\nif (!snap.exists()) {\n  // refresh session cookie / force sign-in instead of updating\n}","typeGuard":"function sessionExists(s: any): s is { sessionToken: string; expires: Date } {\n  return !!s && typeof s.sessionToken === 'string'\n}","tryCatchPattern":"try {\n  await adapter.updateSession({ sessionToken, expires })\n} catch (e) {\n  if ((e as Error).message.includes('Failed to fetch updated session')) {\n    await signOut({ redirect: false }) // dead session\n  } else throw e\n}","preventionTips":["Check session existence before updating","Avoid racing logout and session refresh across tabs","Audit Firestore rules for read/write on the sessions collection","Clear cookies when the session document is gone"],"tags":["firebase","firestore","session","race-condition","auth"],"backgroundTag":"session-record-missing","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}