astro-fastify icon indicating copy to clipboard operation
astro-fastify copied to clipboard

[BUG] - support to formbody is missing

Open AceCodePt opened this issue 1 year ago • 2 comments

I have the following code:

const defineRoutes: DefineFastifyRoutes = (fastify) => {
 fastify.register(import("@fastify/formbody"));
}

I ran: pnpm build node ./dist/server/entry.mjs

This code seems to throw the follwoing:

{"statusCode":500,"error":"Internal Server Error","message":"Response body object should not be disturbed or locked"}

When I had a post request to:

import {
  serverRouteClient,
  serverRouteClientServiceRole,
} from "@supabase/instances";
import type { APIRoute } from "astro";
import { z } from "zod";

export const POST: APIRoute = async ({ request, cookies, url, redirect }) => {
  console.log("signup");
  const supabase = serverRouteClient(cookies);

  const body = await request
    .formData()
    .then(Object.fromEntries)
    .then(
      z.object({
        email: z.string().email(),
        password: z.string(),
        passwordConfirmation: z.string(),
      }).parseAsync,
    );

  if (body.password != body.passwordConfirmation) {
    throw new Error("Confirmation password didn't match the actual password");
  }

  const res = await supabase.auth.signUp({
    email: body.email,
    password: body.password,
    options: {
      emailRedirectTo: `${url.origin}/auth/callback`,
    },
  });

  if (res.error) {
    return new Response(res.error.message, { status: res.error.status });
  }
  if (!res.data.user?.id) {
    return new Response("Failed to create a user", { status: 500 });
  }
  
  return redirect("/");
};

I have the following configuration:

import { defineConfig } from "astro/config";
import react from "@astrojs/react";
import tailwind from "@astrojs/tailwind";
import fastify from "@matthewp/astro-fastify";

// https://astro.build/config
export default defineConfig({
  integrations: [react(), tailwind()],
  output: "server",
  adapter: fastify({
    entry: new URL("./server-entry.ts", import.meta.url),
  }),
  server: {
    port: +(process.env.PORT || 4321),
  },
});

AceCodePt avatar Feb 20 '24 09:02 AceCodePt

Solved with https://github.com/matthewp/astro-fastify/pull/35

AceCodePt avatar Feb 22 '24 11:02 AceCodePt

The problem was that fastify opens data from the socket. The information of the socket can be only read once! Therefor in #35 I reconstructed the original request (or some of it)

AceCodePt avatar Feb 29 '24 20:02 AceCodePt