Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - support to formbody is missing #34

Open
AceCodePt opened this issue Feb 20, 2024 · 2 comments
Open

[BUG] - support to formbody is missing #34

AceCodePt opened this issue Feb 20, 2024 · 2 comments

Comments

@AceCodePt
Copy link
Contributor

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
Copy link
Contributor Author

Solved with #35

@AceCodePt
Copy link
Contributor Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant