sergiodxa / remix-auth

Simple Authentication for Remix
MIT License
1.94k stars 112 forks source link

How to resolve "Response body object should not be disturbed or locked"? #263

Closed adaboese closed 9 months ago

adaboese commented 9 months ago

Describe the bug

Started getting this error when trying to authenticate. I am unsure what's causing it.

> authentication error > TypeError: Response body object should not be disturbed or locked
>     at extractBody (node:internal/deps/undici/undici:5178:17)
>     at new Request (node:internal/deps/undici/undici:6160:48)
>     at Authenticator.authenticate (/Users/adaboese/Developer/adaboese/aimd/node_modules/.pnpm/remix-auth@3.6.0_@remix-run+react@2.4.0_@remix-run+server-runtime@2.4.0/node_modules/remix-auth/build/authenticator.js:68:41)
>     at action (/Users/adaboese/Developer/adaboese/aimd/app/routes/_authentication/sign-in/index.tsx:56:56)
>     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Code around this is super basic and was working before:

export const action = async ({ request }: ActionFunctionArgs) => {
  const formData = await request.formData();

  const submission = parse(formData, {
    schema: FormDataZodSchema,
  });

  if (submission.value && submission.value.intent === 'sign-in') {
    try {
      return await authenticator.authenticate('user-pass', request, {
        context: {
          formData,
        },
        successRedirect: '/app',
        throwOnError: true,
      });

// ...

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

Don't know

Expected behavior

Not get the error

Screenshots or Videos

No response

Platform

Additional context

No response

adaboese commented 9 months ago

Seems like a bug in Remix https://github.com/remix-run/remix/discussions/8315

sergiodxa commented 9 months ago

This is not a bug in Remix, you can only read the request body once, since you’re calling request.formData() and Remix Auth also do it it fails.

If you want to read the body before calling Remix Auth you need to first clone the request with request.clone().formData().

adaboese commented 9 months ago

It was working just fine before migrating remix version:

-    "@remix-run/dev": "^2.3.1",
-    "@remix-run/node": "^2.3.1",
-    "@remix-run/react": "^2.3.1",
-    "@remix-run/serve": "^2.3.1",
+    "@remix-run/dev": "^2.4.0",
+    "@remix-run/node": "^2.4.0",
+    "@remix-run/react": "^2.4.0",
+    "@remix-run/serve": "^2.4.0",

But good to know. Thank you!