{"record":{"id":"15b4f19041f7cb2a","repo":"nextauthjs/next-auth","slug":"createuser-failed-to-fetch-created-user","errorCode":null,"errorMessage":"[createUser] Failed to fetch created user","messagePattern":"\\[createUser\\] Failed to fetch created user","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/adapter-firebase/src/index.ts","lineNumber":104,"sourceCode":"\n  const preferSnakeCase = namingStrategy === \"snake_case\"\n  const C = collectionsFactory(db, preferSnakeCase, {\n    users: \"users\",\n    sessions: \"sessions\",\n    accounts: \"accounts\",\n    verificationTokens: preferSnakeCase\n      ? \"verification_tokens\"\n      : \"verificationTokens\",\n    ...collections,\n  })\n  const mapper = mapFieldsFactory(preferSnakeCase)\n\n  return {\n    async createUser(userInit) {\n      const { id: userId } = await C.users.add(userInit as AdapterUser)\n\n      const user = await getDoc(C.users.doc(userId))\n      if (!user) throw new Error(\"[createUser] Failed to fetch created user\")\n\n      return user\n    },\n\n    async getUser(id) {\n      return await getDoc(C.users.doc(id))\n    },\n\n    async getUserByEmail(email) {\n      return await getOneDoc(C.users.where(\"email\", \"==\", email))\n    },\n\n    async getUserByAccount({ provider, providerAccountId }) {\n      const account = await getOneDoc(\n        C.accounts\n          .where(\"provider\", \"==\", provider)\n          .where(mapper.toDb(\"providerAccountId\"), \"==\", providerAccountId)\n      )","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/adapter-firebase/src/index.ts#L86-L122","documentation":"The Firebase adapter's createUser adds a user document via C.users.add(), then immediately reads the doc back with getDoc() to return it. If the read yields nothing, it throws '[createUser] Failed to fetch created user'. Note: with the firebase Admin SDK, getDoc on a missing path can resolve undefined/null here, so this indicates the write or read round-trip failed unexpectedly.","triggerScenarios":"C.users.add() resolves but the subsequent getDoc(C.users.doc(userId)) returns no document — e.g. Firestore security rules or a triggered onDelete wiping the doc, mixing client SDK and Admin SDK access patterns, or a mapping bug in getDoc returning undefined for an existing doc.","commonSituations":"Custom getDoc helper that returns doc.data() (undefined for empty payloads) instead of the snapshot so freshly created users with empty field sets appear 'missing'; emulator vs production project mismatch; security rules blocking the immediate read.","solutions":["Inspect the custom getDoc helper used by the adapter — ensure it returns the snapshot/data for an existing document and handles doc.exists correctly.","Check Firestore security rules allow reading the users collection for the credentials the adapter uses.","Verify the adapter is configured for the same Firestore project/database that received the write (emulator vs prod).","Retry the read; if a Firestore trigger deletes the user, find and fix that trigger."],"exampleFix":"// before\nconst user = await getDoc(C.users.doc(userId))\nif (!user) throw new Error('[createUser] Failed to fetch created user')\n// after\nconst snap = await C.users.doc(userId).get()\nif (!snap.exists) throw new Error('[createUser] Failed to fetch created user')\nconst user = mapper.fromDb({ id: snap.id, ...snap.data() })","handlingStrategy":"try-catch","validationCode":"const snap = await C.users.doc(userId).get()\nif (!snap.exists) throw new Error(`User doc ${userId} missing after create`)","typeGuard":"function docExists<T>(snap: { exists: boolean; data(): T | undefined }): snap is { exists: true; data(): T } {\n  return snap.exists && snap.data() !== undefined\n}","tryCatchPattern":"try {\n  const user = await adapter.createUser(userInit)\n  return user\n} catch (e) {\n  if ((e as Error).message.includes('Failed to fetch created user')) {\n    console.error('Firestore create/read round-trip failed; check rules & getDoc helper', e)\n    throw e\n  }\n  throw e\n}","preventionTips":["Ensure your getDoc wrapper checks snapshot.exists rather than treating empty data() as missing.","Verify Firestore rules permit reads on users for the adapter's credentials.","Pin emulator vs production project config so writes and reads hit the same database."],"tags":["firebase","firestore","database","not-found"],"backgroundTag":"record-not-found","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}