nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.55k stars 3.45k forks source link

⨯ TypeError: Response body object should not be disturbed or locked #11390

Closed breftejk closed 2 months ago

breftejk commented 3 months ago

Environment

  System:
    OS: macOS 15.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 178.53 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.2.0 - /opt/homebrew/bin/node
    Yarn: 4.3.1 - /opt/homebrew/bin/yarn
    npm: 10.7.0 - /opt/homebrew/bin/npm
    Watchman: 2024.05.06.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 18.0

Reproduction URL

https://github.com/Discord-Dashboard/Base-Theme

Describe the issue

Running next on external server, you cannot use next-auth. It will work, but the callback causes it to... break.

const fastify = dashboard.fastify;

const app = Next({
     dev: true,
     port: dashboard.config.port,
     dir: join(__dirname, '../'),
});
const handle = app.getRequestHandler();
await app.prepare();

fastify.all('*', (request, reply) => {
     return handle(request.raw, reply.raw);
});

Error:

 ⨯ TypeError: Response body object should not be disturbed or locked
    at extractBody (node:internal/deps/undici/undici:4151:17)
    at new Request (node:internal/deps/undici/undici:5084:48)
    at new NextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/request.js:33:14)
    at NextRequestAdapter.fromNodeNextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:94:16)
    at NextRequestAdapter.fromBaseNextRequest (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/web/spec-extension/adapters/next-request.js:70:35)
    at doRender (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1376:73)
    at cacheEntry.responseCache.get.routeKind (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1587:46)
    at ResponseCache.get (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/response-cache/index.js:49:26)
    at DevServer.renderToResponseWithComponentsImpl (/Users/marcinkondrat/Projects/AssistantsTechnologies/Discord-Dashboard/node_modules/next/dist/server/base-server.js:1507:53)
 POST /api/auth/callback/credentials 500 in 5ms

BTW: Both next.js and fastify are working just fine, the problem is with the way next-auth proceeds with the POST callback request.

How to reproduce

Expected behavior

It should proceed with the callback.

dangluan343 commented 3 months ago

@breftejk I have the same issue. Does anyone have the solution?

iMidnights commented 2 months ago

Same here! Anyone able to solve it?

Gjum commented 2 months ago

I solved this by moving the Next request handler into onRequest (before Fastify parses the body):

    fastify.all('*', {
        // nextjs does its own POST body parsing, must run before fastify
        onRequest: async (req, res, next) => {
            res.hijack()
            try {
                await preparedP
                const parsedUrl = URL.parse(req.url, true)
                await handleNextRequest(req.raw, res.raw, parsedUrl)
            } catch (err) {
                console.error('Error in', req.url, err)
                res.statusCode = 500
                res.raw.end('Internal server error')
            }
            next()
        },
        handler: () => {},
    })

This can probably cleaned up further, but it works.

balazsorban44 commented 2 months ago

Closing per the above comment. Sounds like an upstream issue.