next-auth: Error when fetching session in API
Hello again!
I’m having a problem when fetching a session in an api route. I get this error:
app_1 | CLIENT_FETCH_ERROR https://localhost:3000/api/auth/session FetchError: request to https://localhost:3000/api/auth/session failed, reason: write EPROTO 140431569815360:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:
app_1 |
app_1 | at ClientRequest.<anonymous> (/app/node_modules/next/dist/compiled/node-fetch/index.js:1:147710)
app_1 | at ClientRequest.emit (events.js:310:20)
app_1 | at TLSSocket.socketErrorListener (_http_client.js:426:9)
app_1 | at TLSSocket.emit (events.js:310:20)
app_1 | at errorOrDestroy (internal/streams/destroy.js:108:12)
app_1 | at onwriteError (_stream_writable.js:424:5)
app_1 | at onwrite (_stream_writable.js:445:5)
app_1 | at internal/streams/destroy.js:50:7
app_1 | at TLSSocket.Socket._destroy (net.js:677:5)
app_1 | at TLSSocket.destroy (internal/streams/destroy.js:38:8)
app_1 | at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:93:12) {
app_1 | type: 'system',
app_1 | errno: 'EPROTO',
app_1 | code: 'EPROTO'
app_1 | }
app_1 | session null
The code is simple:
import { session as getSession } from 'next-auth/client'
export default async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession({ req });
console.log('session', session);
...
Any pointers for how I can debug this? I’m not sure where to start.
Thanks!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 24 (17 by maintainers)
I had to do add this
NEXTAUTH_URL_INTERNAL='http://127.0.0.1:3000'Note that it it only seemed to work with the IP address. It did not work with ‘localhost’
Hmm I think you might want to use
http://localhostinstead ofhttps://localhostunless you have HTTPS set up (but it does work if you do have HTTPS setup).If you DO have HTTPS set up it might be that your browser isn’t able to validate your locally signed certificate.
Hey this MIGHT work for testing locally:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";@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.
HAPROXY Config The configuration below gets an A+ on https://www.ssllabs.com/
Dockerfile
haproxy.cfg
Error when running Next-Auth-Example
To get around this error on development.
Oh neato!
So you can set
process.env.NEXTAUTH_SITEtohttp://localhost:3000URL and thesession()method will use that.When calling
session()on the server, this env var just needs to be set before you call it, like this:process.env.NEXTAUTH_SITE = "http://localhost:3000"You can also set it in
nextauth.config.jsas a clientenvif you want the browser to do that, but in this case I don’t think you need to do that, as it only needs to be set when called server side in your setup, I think?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.
Oh sure, I appreciate that is kludgy but you can totally just load the database provider directly and call methods from it.
e.g.
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:
However I think I would see if there is a way you can set
NEXTAUTH_SITEto something other endpoint (just HTTP but on a different port) that the server can call?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.
So, to fix this, I have some options:
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): app nginx
only nginx is exposed and routes all requests wherever, and handles ssl. app receives http requests, but all the https parts work thanks to nginx.
When the session gets requested by app itself, it uses baseUrl, which is
https://localhost:3000. Since node doesn’t have ssl configured, it errorsSo 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