Fixes request body handling in this library #72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Today I spend a lot of time debugging this library. The requirements I had to meet was to enable passkeys and password registration and login. I had a lot of issues because of the forwarded request body and content-type header.
Registering with a passkey resulted in a HTTP-Request originated from this library which had a Body in JSON-Format but a Content-Type Header of application/form-data. This, obviously, resulted in the ory kratos server not being able to parse the body correctly. I had to change the content-type header to application/json and the body to a stringified JSON-Object (https://github.com/ory/kratos/blob/master/selfservice/flow/registration/decoder.go#L32)[https://github.com/ory/kratos/blob/master/selfservice/flow/registration/decoder.go#L32]
I stumbled upon something which looks like a bug to me.
The documentation states, one should disable the body parsing https://github.com/ory/integrations/blob/main/README.md#nextjs
This is in direct contradiction to the code in the library. The createdApiHandler function stringifies the req.body (https://github.com/ory/integrations/blob/main/src/next-edge/index.ts#L115). Which is by definition undefined if the body parsing is disabled, since one has todo it manually.
There is a issue with quite some comments which is related to this problem: #29
The content type is zero, since the req.body is undefined. The solution to this ticket was to enable body parsing again by just stopping to export the config object. This is a workaround and not a solution to the problem: #29 (comment)
My Issue:
I did exactly like the comment asked me todo, but if you enable passkeys, you are actually supposed to send form-data and not a JSON-Object.
Since Body-Parsing is enabled again, nextjs is going to parse the form-data into a javascript object. The provided content-type header is
application/form-data
, which is correct. But know this library copies the application/form-data header and JSON.stringifies the request body, resulting in an invalid combination.The fix is super simple: Just read the raw body and forward it. No json stringify. This keeps the content-type header and the body-format in sync. I think this is the intended behavior of the library, since the documentation states that the body parsing should be disabled.