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
Reuse previous session in SessionProvider
#4252
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/nextauthjs/next-auth/J9si6EAjkZHiUcmMZFk1Scn5JzdF [Deployment for 43245a6 canceled] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should also update the hasInitialSession
value with && __NEXTAUTH._session !== undefined
to take this previous value's existence into consideration:
Otherwise, an existing session might be reset if props.session
was null
, and the loading
state would start with true
for that context, even though there is a session already.
I am not entirely sure of the gains with multiple SessionProvider
though. 🤔 Could you explain? Right now it just sounds like a bit of overcomplicating things.
Yes, you are right. I updated the commit so the "props session" has the highest priority and then the old client-side session.
The idea is not to use multiple SessionProvider but to instead only use the SessionProvider on some pages. So the landing But in the The problem this PR fixes is, if somebody navigates from Also, I use puppeteer to generate screenshots of pages for preview images. And I don't want this headless worker to generate a bunch of session requests. |
Reasoning 💡
This reuses the previous session from the last mounted
<SessionProvider>
, if present.With this change, users don't have to add the
<SessionProvider>
to the_app.js
but can instead add it to individual pages that require authentication.This reduces bundle size for pages that do not require authentication (e.g. landing pages, documentation) and does not create cookies until the user enters a page that uses the provider.
Technically this was possible before, but if the user went from a non-authenticated page back to a authenticated page, the session would not be loaded.
Checklist 🧢
Affected issues 🎟