-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Error when fetching session in API #212
Comments
Hmm I think you might want to use If you DO have HTTPS set up it might be that your browser isn't able to validate your locally signed certificate. |
I think I found the issue. I think there needs to be a separate function to fetch the session for the server-side. The error is because it looks like the server requests the session object through a http request, instead of using the DB. I have this behind an nginx ingress and inside a docker container. |
So the cert authority that I'm using is on my machine (so ssl works in browser), while node can't verify the certs? I'll dig a bit deeper and check the certs |
Yeah, that sound about right, I think it's probably a consequence of the configuration here. The NextAuth.js client uses the build in @geraldnolan might be able to help with this, he wrote the documentation for apple which needs HTTPS to run locally and might have solved this :-) PS: The |
Hey this MIGHT work for testing locally:
|
Okay, found what the issue is. Here's how I have things set up: docker-compose has 2 containers (will have more once I set everything up): only nginx is exposed and routes all requests wherever, and handles ssl. When the session gets requested by app itself, it uses baseUrl, which is |
So, to fix this, I have some options:
|
Oh neato! So you can set When calling
You can also set it in |
(Note in production everything should be fine; the example site has server side calls to session; it should just work normally when everything has a 'real' cert.) |
Thanks! By setting NEXTAUTH_SITE I now get this error as a response:
|
So it looks like the best solution is to have a server-side method |
I see that is Would it be sane to write a wrapper around that to get the session? |
Oh sure, I appreciate that is kludgy but you can totally just load the database provider directly and call methods from it. e.g. import Adapters from `next-auth/adapters`
const adapter = Adapters.Default(process.env.DATABASE_URL)
const db = await adapter.getAdapter({ /* options */ } )
const { getSession } = db
const sessionTokenCookieName = '__Secure-next-auth.session-token'
const sessionToken = req.cookies[sessionTokenCookieName]
// getSession(sessionToken) BTW: Just checking if you are using JWT or database sessions? If you are using JWT instead of database sessions, then you can get the session from the cookie, like this: const jwtSecret = 'your secret' // // see docs for basic options
const sessionTokenCookieName = '__Secure-next-auth.session-token'
const sessionMaxAge = 30 * 24 * 60 * 60 * 1000 // see docs for basic options
const sessionToken = req.cookies[sessionTokenCookieName]
const token = jwt.verify(sessionToken, jwtSecret, { maxAge: sessionMaxAge }) However I think I would see if there is a way you can set Maybe there there an internal IP or hostname you could set it to that would mean it doesn't try to connect to the nginx instance? I think as it only needs to be set for local development (and maybe in a test environment, if you have one) that would be easier. |
Thanks @iaincollins ! Yeah, I'll try to get the redirect working before doing this. |
@TwoAbove Can you share your nginx configuration? |
@iaincollins and @iaincollins I was doing some additional testing to see if I could mimic the error @TwoAbove was getting and the same was happening using HAPROXY. version: '3'
services:
next-auth-example:
build:
context: ./
dockerfile: Dockerfile
restart: unless-stopped
ports:
- '3000:3000'
haproxy:
build:
context: ./haproxy
dockerfile: Dockerfile
links:
- next-auth-example
restart: unless-stopped
ports:
- 80:80
- 443:443
- 1936:1936
HAPROXY Config The configuration below gets an A+ on https://www.ssllabs.com/ Dockerfile FROM haproxy:1.7
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
COPY /configs/ssl/private/localhost.pem /usr/local/etc/private/localhost.pem haproxy.cfg
Error when running Next-Auth-Example next-auth-example_1 | CLIENT_FETCH_ERROR https://localhost/api/auth/session FetchError: request to https://localhost/api/auth/session failed, reason: connect ECONNREFUSED 127.0.0.1:443
next-auth-example_1 | at ClientRequest.<anonymous> (/usr/src/app/node_modules/next/dist/compiled/node-fetch/index.js:1:147710)
next-auth-example_1 | at ClientRequest.emit (events.js:310:20)
next-auth-example_1 | at TLSSocket.socketErrorListener (_http_client.js:426:9)
next-auth-example_1 | at TLSSocket.emit (events.js:310:20)
next-auth-example_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
next-auth-example_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
next-auth-example_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21) {
next-auth-example_1 | type: 'system',
next-auth-example_1 | errno: 'ECONNREFUSED',
next-auth-example_1 | co To get around this error on development.
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
|
Is there any update on this server-only method @iaincollins ? It would be super useful for us, as it would effectively half our Lambda invocations (every single API endpoint always needs to call the "/session" endoint, resulting in two Lambda invocations). Thank you! ❤️ |
Anything here @iaincollins? We could really use a server-side getSession utility. It makes no sense to make another api request from our api just to get the session. We just need to load it directly from the db. |
See a reasoning here: #947 (comment) |
This worked for me, im using a custom server with a self signed cert to get HTTPS on localhost :) |
I had to do add this
Note that it it only seemed to work with the IP address. It did not work with 'localhost' |
Thanks for posting this, it solved this issue with an app running on a kubernetes cluster 🙌 🎉 |
I did a short piece on how this problem can be solved using a reverse proxy together with your own root CA signed certificates. Maybe it can be of help to someone struggling with this. https://risaksson.com/post/10/2022-05-13/HTTPS-enabled-development-environments |
You saved the day. Thank you! |
Hello again!
I'm having a problem when fetching a session in an api route. I get this error:
The code is simple:
Any pointers for how I can debug this? I'm not sure where to start.
Thanks!
The text was updated successfully, but these errors were encountered: