payloadcms / payloadcms/payload
addSessionToUser fails on auth collections with localized fields (MongoDB CastError)
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 44.8k
- Forks
- 4.2k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 53
Description
Describe the Bug
addSessionToUser (exported from payload/shared) passes the entire user document to payload.db.updateOne. On MongoDB, when the auth collection has localized: true fields, this fails because findByID returns flattened locale values (e.g. displayName: "Test User") while Mongoose expects the embedded locale object (e.g. { en: "Test User", es: "..." }).
The same pattern exists in revokeSession.
Link to the code that reproduces this issue
https://github.com/MurzNN/payload-issues/tree/issue/add-session-to-user-localized-fields
Reproduction lives in test/_community/ (config.ts, int.spec.ts, README.md).
Reproduction Steps
- Clone the reproduction branch:
git clone --branch issue/add-session-to-user-localized-fields https://github.com/MurzNN/payload-issues.git
cd payload-issues
pnpm install
- Start MongoDB and run the integration test:
pnpm docker:start mongodb
PAYLOAD_DATABASE=mongodb pnpm test:int _community
- The test
reproduces CastError when addSessionToUser writes a flattened user documentfails with:
CastError: Cast to Embedded failed for value "Test User" (type string) at path "displayName"
ObjectParameterError: Parameter "obj" to Document() must be an object, got "Test User" (type string)
Root cause (packages/payload/src/auth/sessions.ts):
await payload.db.updateOne({
id: user.id,
collection: collectionConfig.slug,
data: user, // entire document — includes flattened localized fields
req,
returning: false,
})
Suggested fix: update only changed fields:
await payload.db.updateOne({
id: user.id,
collection: collectionConfig.slug,
data: {
sessions: user.sessions,
updatedAt: null,
},
req,
returning: false,
})
Which area(s) are affected?
- area: core
- db: mongodb
Environment Info
Payload: 3.85.2 (also reproduced against payload monorepo main / 4.0.0-beta.0)
Database adapter: @payloadcms/db-mongodb
Localization: enabled (en, es)
Node.js: 22.x
Encountered while implementing custom OTP login using Payload session/JWT helpers (addSessionToUser after findByID).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with packages/payload/src/auth/sessions.ts and inspect addSessionToUser and revokeSession, then run the reproduction in test/_community/ using the documented MongoDB integration-test commands. Done means localized auth collections no longer produce a MongoDB CastError when either session helper updates a user, with the existing reproduction passing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, typescript
- Domain
- authentication, backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100