Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New session cookie generated every time /session is called #1934

Closed
ramiel opened this issue May 5, 2021 · 5 comments
Closed

New session cookie generated every time /session is called #1934

ramiel opened this issue May 5, 2021 · 5 comments
Labels
question Ask how to do something or how something works

Comments

@ramiel
Copy link
Contributor

ramiel commented May 5, 2021

Description 🐜

Everytime the application page is loaded a call to /session is done. This sets a new cookie even if the old cookie is not expired. I'm wondering if this behaviour is wrong and no cookie should be set if one valid is already available. Same happens when the session is checked after the setup time interval

How to reproduce ☕️

You can simply see this happening in the demo app

Screenshots / Logs 📽

The set-cookie always happens

Screenshot_20210505_131605

Environment 🖥

Please run this command:

$ npx envinfo --system --binaries --browsers --npmPackages "{next-auth}"

System:
OS: Linux 5.4 KDE neon 5.21
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Memory: 666.77 MB / 15.51 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 14.16.1 - /usr/bin/node
Yarn: 1.22.10 - ~/.npm-global/bin/yarn
npm: 6.14.12 - /usr/bin/npm
Browsers:
Brave Browser: 90.1.23.75
Chrome: 90.0.4430.93
Firefox: 88.0
npmPackages:
next-auth: ^3.15.5 => 3.15.9

Contributing 🙌🏽

It takes a lot of work 🏋🏻‍♀️ maintaining a library like next-auth; any contribution is more than welcome 💚

In case you're willing to help fix this bug, please let us know here, and we'll reach you 😊 . Otherwise, you can have a look at the issues labelled with "good first issue" and pick any of them.

@ramiel ramiel added the bug Something isn't working label May 5, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented May 5, 2021

You have to use some kind of caching for the session. See the Provider docs

If you use react-query, this might also be interesting: https://github.com/nextauthjs/react-query

@balazsorban44 balazsorban44 added question Ask how to do something or how something works and removed bug Something isn't working labels May 5, 2021
@ramiel
Copy link
Contributor Author

ramiel commented May 5, 2021

Thanks for the reply but that probably doesn't answer the initial question: why a new session is created if the old one is not expired?

@balazsorban44
Copy link
Member

When you don't cache the response, it means that every call of useSession will contact the api.

And the API does update the expiry time as seen here:

https://github.com/nextauthjs/next-auth/blob/main/src/server/routes/session.js

This is to keep your session alive. Happens in both cases when you use JWT or database sessions as well.

@bfourgeaud
Copy link

Does someone have a sample code to cache the session ? I have not found any viable solution and set-cookie is still called at every page load ...
I tried unstable_cache from nextjs but nothing improved ...

@jzzj
Copy link

jzzj commented Dec 26, 2024

Does someone have a sample code to cache the session ? I have not found any viable solution and set-cookie is still called at every page load ... I tried unstable_cache from nextjs but nothing improved ...

Same issue, have you found any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

4 participants