{"record":{"id":"3c70af96a465d7b3","repo":"nextauthjs/next-auth","slug":"userid-is-undef-in-createsession","errorCode":null,"errorMessage":"userId is undef in createSession","messagePattern":"userId is undef in createSession","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/adapter-neon/src/index.ts","lineNumber":172,"sourceCode":"        account.userId,\n        account.provider,\n        account.type,\n        account.providerAccountId,\n        account.access_token,\n        account.expires_at,\n        account.refresh_token,\n        account.id_token,\n        account.scope,\n        account.session_state,\n        account.token_type,\n      ]\n\n      const result = await client.query(sql, params)\n      return result.rows[0]\n    },\n    async createSession({ sessionToken, userId, expires }) {\n      if (userId === undefined) {\n        throw Error(`userId is undef in createSession`)\n      }\n      const sql = `insert into sessions (\"userId\", expires, \"sessionToken\")\n      values ($1, $2, $3)\n      RETURNING id, \"sessionToken\", \"userId\", expires`\n\n      const result = await client.query(sql, [userId, expires, sessionToken])\n      return result.rows[0]\n    },\n\n    async getSessionAndUser(sessionToken: string | undefined): Promise<{\n      session: AdapterSession\n      user: AdapterUser\n    } | null> {\n      if (sessionToken === undefined) {\n        return null\n      }\n      const result1 = await client.query(\n        `select * from sessions where \"sessionToken\" = $1`,","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/adapter-neon/src/index.ts#L154-L190","documentation":"The Neon adapter's createSession guards that userId is defined before inserting into the sessions table, throwing a plain Error otherwise. It protects against writing a NULL 'userId' FK. Receiving undefined here means the caller passed a malformed AdapterSession without a userId.","triggerScenarios":"Calling adapter.createSession({ sessionToken, userId: undefined, expires }) — typically from a session callback/DB session flow where the user row (and thus user.id) was undefined or the createUserServiceUser step silently failed.","commonSituations":"Custom code constructing session objects manually; a user create step returning undefined (e.g. mapping mismatch in a custom adapter chain); JWT/strategy misconfiguration where sessions are created without a bound user; upstream user id field named differently (e.g. user_id) so id is undefined.","solutions":["Inspect where the session is created and ensure the user record exists with a defined id before createSession is called","Verify your user-mapping function returns the adapter's AdapterUser shape ({ id, ... }) not a raw DB row with different column names","Log the incoming arguments in a wrapper adapter to identify which flow passes userId: undefined","If you have a custom user provider/adapter, confirm createUser returns the inserted row"],"exampleFix":"// before\nawait adapter.createSession({ sessionToken, userId: user.id, expires }) // user.id undefined\n// after\nif (!user?.id) throw new Error(`Cannot create session: user not persisted (id missing)`)\nawait adapter.createSession({ sessionToken, userId: user.id, expires })","handlingStrategy":"type-guard","validationCode":"if (session?.userId === undefined) throw new Error('createSession requires a persisted user id')","typeGuard":"function hasUserId(s: { userId?: unknown }): s is { userId: string } {\n  return typeof s.userId === 'string' && s.userId.length > 0\n}","tryCatchPattern":"try {\n  await adapter.createSession({ sessionToken, userId, expires })\n} catch (e) {\n  if (e instanceof Error && e.message === 'userId is undef in createSession') {\n    throw new Error('User was not persisted before session creation', { cause: e })\n  }\n  throw e\n}","preventionTips":["Always createUser before createSession in custom flows","Map DB rows to AdapterUser with an explicit id field","Add adapter-level unit tests for the database session strategy","Use database-session strategy only when user binding is guaranteed"],"tags":["adapter","neon","session","database"],"backgroundTag":"adapter-undefined-userid","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}