nextauthjs/next-auth · error

[createUser] Failed to fetch created user

Error message

[createUser] Failed to fetch created user

What it means

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.

Source

Thrown at packages/adapter-firebase/src/index.ts:104

  const preferSnakeCase = namingStrategy === "snake_case"
  const C = collectionsFactory(db, preferSnakeCase, {
    users: "users",
    sessions: "sessions",
    accounts: "accounts",
    verificationTokens: preferSnakeCase
      ? "verification_tokens"
      : "verificationTokens",
    ...collections,
  })
  const mapper = mapFieldsFactory(preferSnakeCase)

  return {
    async createUser(userInit) {
      const { id: userId } = await C.users.add(userInit as AdapterUser)

      const user = await getDoc(C.users.doc(userId))
      if (!user) throw new Error("[createUser] Failed to fetch created user")

      return user
    },

    async getUser(id) {
      return await getDoc(C.users.doc(id))
    },

    async getUserByEmail(email) {
      return await getOneDoc(C.users.where("email", "==", email))
    },

    async getUserByAccount({ provider, providerAccountId }) {
      const account = await getOneDoc(
        C.accounts
          .where("provider", "==", provider)
          .where(mapper.toDb("providerAccountId"), "==", providerAccountId)
      )

View on GitHub (pinned to a1a16a5a77)

Solutions

  1. Inspect the custom getDoc helper used by the adapter — ensure it returns the snapshot/data for an existing document and handles doc.exists correctly.
  2. Check Firestore security rules allow reading the users collection for the credentials the adapter uses.
  3. Verify the adapter is configured for the same Firestore project/database that received the write (emulator vs prod).
  4. Retry the read; if a Firestore trigger deletes the user, find and fix that trigger.

Example fix

// before
const user = await getDoc(C.users.doc(userId))
if (!user) throw new Error('[createUser] Failed to fetch created user')
// after
const snap = await C.users.doc(userId).get()
if (!snap.exists) throw new Error('[createUser] Failed to fetch created user')
const user = mapper.fromDb({ id: snap.id, ...snap.data() })
Defensive patterns

Strategy: try-catch

Validate before calling

const snap = await C.users.doc(userId).get()
if (!snap.exists) throw new Error(`User doc ${userId} missing after create`)

Type guard

function docExists<T>(snap: { exists: boolean; data(): T | undefined }): snap is { exists: true; data(): T } {
  return snap.exists && snap.data() !== undefined
}

Try / catch

try {
  const user = await adapter.createUser(userInit)
  return user
} catch (e) {
  if ((e as Error).message.includes('Failed to fetch created user')) {
    console.error('Firestore create/read round-trip failed; check rules & getDoc helper', e)
    throw e
  }
  throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of nextauthjs/next-auth@a1a16a5a77 (2026-08-28). Data as JSON: /api/errors/15b4f19041f7cb2a. Report an issue: GitHub.