fix: preserve multi-instance session data on login/logout with tests …#1394
Open
fix: preserve multi-instance session data on login/logout with tests …#1394
Conversation
…and improvements type safe in test file
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.
Proposal:
When using multiple
Authenticatorinstances withclearSessionOnLogin: false, logging in or out with one instance would inadvertently destroy the session data of other instances.This happened because
request.session.regenerate()was called without arguments in bothlogInandlogOut, which wipes the entire session — including passport keys belonging to other instances (e.g.passport-abeing lost when logging in viapassport-b).Changes Made:
In
SecureSessionManager:logIn: theelsebranch (triggered whenclearSessionOnLogin: false) calledrequest.session.regenerate()with no arguments, destroying all existing session data.logOut: similarly calledrequest.session.regenerate()unconditionally, logging out all instances instead of just the current one.When
clearSessionOnLoginisfalse, instead of callingregenerate()blindly:(request.session as any).data?.()before regeneratingrequest.session.regenerate()to get a new secure session IDrequest.session.set(), omitting only the key being logged out (inlogOut)Updated tests with better type checking
Update
@fastify/secure-sessiondependency in thepackage.jsonNote:
session.data()is used becauseObject.keys(request.session)only returns internal metadata fields (changed,deleted), not the actual session data. The method is available on both@fastify/sessionand@fastify/secure-session, though only declared in the types of the latter.This PR should close this PRs: