question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unstable_getServerSession Not Updating Session After Redirect

See original GitHub issue

Environment

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

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 image

When I redirect, the dashboard session gets the proper name and session. image

However, on the page it does not appear correctly until I refresh. image

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:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
joepetrillocommented, Aug 27, 2022

It seems adding the following event forces the session to refresh.

  const event = new Event('visibilitychange');
  document.dispatchEvent(event);

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.

0reactions
stale[bot]commented, Nov 22, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Session not updating after redirect - node.js - Stack Overflow
Sessions are not automatically saved by res.redirect . And req.session.save() does not return a promise but requires a callback function:
Read more >
Client API - NextAuth.js
The session data returned to the client does not contain sensitive information such as the Session Token or OAuth tokens.
Read more >
Implementing federated sign out when using next-auth in you ...
After the thrid party provider has signed the user out, the federated auth server will redirect back to a route in the Next...
Read more >
NextAuth - Google And GitHub Authentications for Nextjs
What is NextAuth? Why use NextAuth.js? Project setup; Create API Routes; Configure Shared Session State; Update Page Components; Add React Hook ...
Read more >
Next.js Authentication with NextAuth, tRPC and Prisma ORM
And let's add the following schema to our schema.prisma : ... ie the session data must be stored in a Json Web Token...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found