fix(auth): avoid persisting populated relationship fields in OAuth session write#75
Open
JannikZed wants to merge 2 commits into
Open
Conversation
Contributor
Author
|
@WilsonLe found this bug with the new session logic. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a regression in OAuth callback session persistence where the plugin writes the full
userobject viadb.updateOnewhen adding a Payload session.In projects with populated relationship fields (e.g.
roles) on the user during login hooks, this can persist relationship objects instead of IDs and trigger Mongo cast errors like:Cast to [ObjectId] failed ... at path "roles.0"Root Cause
addPayloadSessionToUsercurrently passes a user-shaped object into session persistence, which can include populated relationship data from login hooks.Fix
Update
addPayloadSessionToUserto persist only session-related fields:sessionsupdatedAtwhile still mutating the in-memory user with session info for downstream token signing.
Tests
Added regression coverage in
test/callback-endpoint.spec.ts:does not persist populated relationship fields when creating Payload sessionsThis test simulates a populated
rolesarray injected inbeforeLoginand asserts thatdb.updateOnepayload does not includeroles.Verification
Ran targeted test suite:
pnpm test -- callback-endpoint.spec.ts