[discussion] Add support for anonymous sessions #3413
Closed
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.
Reasoning 💡
Some sites, especially e-commerce, use anonymous tokens to support actions for users that are not logged in. These are then exchanged when the user logs in. See #568 for discussion.
At the moment, NextAuth has no notion of an anonymous session. As part of a poc for adopting the library, I've had a look at the smallest possible change to add it. Whilst we can obtain these tokens outside of the library, it simplifies the design of the poc quite a bit.
I have modified the
session
API route to invoke thejwt
callback if no session cookie is already present. In this case,token
will be empty anduser
,profile
etc undefined. The callback can then modify the token, in our case adding an anonymous access token.I've added an
anonymousSession
setting underjwt
to opt in, as this changes behaviour.This PR is not ready to merge but I'd like to raise it early and start a discussion about whether this feature is right for NextAuth generally and if this is the right way to implement it. I'm more than happy to take on any additional work to refine the implementation :)
I've already got a few questions:
GET session
API route?anonSession
API route that the client calls, passing a CSRF token?Checklist 🧢
Affected issues 🎟
Fixes #568