-
Notifications
You must be signed in to change notification settings - Fork 389
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
getSession() on API route returns null when also using getSession() in middleware on Vercel #1085
Comments
Hi @babysteps - thanks for raising this I see you're logging multiple cookies ( I'm asking because there's been a couple of issues recently with Next.js and multiple cookies vercel/next.js#38302 Also, do you have a HAR file (with secrets redacted) or a running example I can log into and take a look at? |
@adamjmcgrath let me get you an example you can log into. however, in the meantime you bring up an interesting point. i just tried logging in using the version on vercel running fine on v2.0.0-beta.2 and it only creates a single cookie. however, logging in with the same user on a different instance running v2.2.1 is creating the double cookie. all of my AUTH env vars are the same for both instances (except for the base url), and obviously the user data is the same, so maybe there is also a clue in that? it does seem reasonable that the double cookie is the issue, but i can't understand why the same user would create different sized cookies. please email me [email protected] so that i can send credentials. |
@adamjmcgrath i just a little more testing around this and found that when logging in with v2.2.1 it is including the idToken, where v2.0.0-beta.2 is not. i seem to remember seeing somewhere in the docs that the default is now to NOT include the idToken, however, that doesn't seem to be correct. i'll keep digging through the docs to find where i saw that, but removing the idToken will likely bring my cookie size down. |
@adamjmcgrath i found it here #809. i added the env var and now i don't get a giant cookie, and it works on vercel. fyi, you should add that var to the docs https://auth0.github.io/nextjs-auth0/types/config.ConfigParameters.html technically, this is still an issue, and i looked at that link to vercel that you posted. it has had this issue for a VERY long time.... sure glad i don't need the idToken (for now...) lastly, while i was just testing, i noticed that |
@babysteps nice find! i've been getting 404s for |
@babysteps - thanks for those replies
I still think it's related to that, I've emailed you and will be able to confirm when I debug it
Thanks for catching this, I've raise a PR to fix #1091 |
thanks for looking into everything @adamjmcgrath. i have already added the env var at this point, i don't know that you will be able to gain much from testing my app. since w/ the single cookie there are now no issues. i have done extensive testing trying to resolve this issue and it pretty clearly points to the "double cookie issue". without any other changes, switching between single cookie and double cookie (by disabling/enabling the idToken) shows that it works/doesn't work with only that difference. happy to keep debugging this with you, but from my POV it's pretty clear what the culprit is. |
Thanks @babysteps Yep, Next.js have a new issue for this vercel/next.js#46579 which I'm sure is the culprit. Will close this in favour of that. |
Hello! This is pretty late, but i had a similar issue recently. Everything was fine when running locally, but on Vercel, my first api call was successful with getSession being able to get the user (because the cookie exists before this call) but subsequent calls failed with getSession returning null (because the cookie was lost/deleted). I am not using getSession in my APIs but only in the middleware. The res from the getSession line has cookies and set-cookie headers. On inspection I found that on Vercel, and I'm assuming getSession works by relying on cookies, using NextResponse.next() meant that the ResponseCookies was empty and there was no set-cookies header. I believe what's happening is that not utilising the res and passing it to NextResponse.next() either means that previous cookie is lost or overwritten (not sure about that). However, if middleware returns NextResponse.next(res), then the cookie is passed forward to be utilised by newer requests. The cookie exists prior to calling getSession() but after it the NextResponse being returned without explicitly passing the cookie deletes it probably. So, in your case passing the res to the NextResponse.redirect() might be the solution by explicitly passing the session cookie. This cookie can allow getSession to return the user in your API. This solved the problem for me. |
@wasifhassan101 Any chance you can share an snippet with us? I know it's a long shot, I'm really curious though |
Checklist
Description
@adamjmcgrath My application is using
getSession()
both in middleware, and also on the server on an API route.When using v2 of the SDK I am getting a
null
value returned fromgetSession()
in my API route. Couple interesting things to note:getSession()
check in the middleware, thegetSession()
response in my API call works fine.getSession()
, but oncegetSession()
is called, I get the null response and the cookies are deleted.getSession()
in middleware andgetSession()
andgetAccessToken()
in my API code.So the flow is that the user goes to the site, middleware checks their auth, if they are authed they are delivered the page. If not, they are redirected to login. Once the user is logged in and on the site, the API route is called periodically from the client using
useSWR()
Below you will see the code for my middleware and for my API route.
Thanks in advance for your help. Been banging my head again the wall for a while with this one.
Reproduction
middleware.js
/pages/api/auth/session.js
getSiteSession.js - called from /api/auth/session
SDK version
2.2.1
Next.js version
13.2.0
Node.js version
18.x
The text was updated successfully, but these errors were encountered: