unstable_getServerSession Not Updating Session After Redirect
See original GitHub issueEnvironment
System: OS: macOS 12.5 CPU: (10) arm64 Apple M1 Max Memory: 2.42 GB / 32.00 GB Shell: 5.8.1 - /bin/zsh
Binaries: Node: 16.16.0 - ~/Library/Caches/fnm_multishells/15888_1661372974805/bin/node npm: 8.11.0 - ~/Library/Caches/fnm_multishells/15888_1661372974805/bin/npm
Browsers: Chrome: 104.0.5112.101 Firefox Developer Edition: 104.0 Safari: 15.6
npmPackages: next: 12.2.5 => 12.2.5 next-auth: ^4.10.3 => 4.10.3 react: 18.2.0 => 18.2.0
Reproduction URL
https://github.com/joepetrillo/lone-link
Describe the issue
I am using the email login provider. I need usernames for my application, so I redirect users who do not have one to a setup profile page. This page simply does some validation of input and updates the name field in my db for the currently logged in user. (null by default) A user will always be redirected here until their name is not null or empty.
Once this completes, I redirect from this setup page to the dashboard page using next router. router.push("/dashboard");
Both the setup page and the dashboard page are getting the session on the server using unstable_getServerSession
.
I then pass the session as a page prop and use useSession()
to get the session. In the get getServerSideProps
function I logged the session retrieved before passing it as a page prop.
See this video https://user-images.githubusercontent.com/32227357/186795104-5a7b6eaf-c095-47a3-96eb-5fc1fb0b8905.mov
As expected the first session on the setup page has a null name
When I redirect, the dashboard session gets the proper name and session.
However, on the page it does not appear correctly until I refresh.
I have been trying to figure this out for a while now, any help is greatly appreciated but I am thinking it could be a bug so wanted to report here.
Thanks!
How to reproduce
Redirect from one page to another that both retrieve the session on the server side.
Expected behavior
The dashboard page should not have to refresh to get access to the session.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
It seems adding the following event forces the session to refresh.
If I add this right before redirecting, it seems to work as a temporary workaround. Is there a more official way to make this work? All of the existing discussions I have found have lead me nowhere unfortunately.
To keep things tidy, we are closing this issue for now. If you think your issue is still relevant, leave a comment and we might reopen it. Thanks!