Having a hard time integrating better-auth and fastify

Hello, I have trouble integrting better-auth, especailly due to the fact that I get absolutely no errors



here's some context

const createAuth = () => {
  return betterAuth({
    database: drizzleAdapter(getDatabase(), {
      provider: "pg",
    }),
    secret: process.env.AUTH_SECRET!,
    baseURL: process.env.API_URL!,

    emailAndPassword: {
      enabled: true,
      requireEmailVerification: false,
    },

    session: {
      expiresIn: 60 * 60 * 24 * 7, // 7 days
      updateAge: 60 * 60 * 24, // 1 day
    },
  });
};

export default createAuth;





My plugin

import { type FastifyPluginAsync } from "fastify";
import fp from "fastify-plugin";
import createAuth from "@/lib/auth";

declare module "fastify" {
  interface FastifyInstance {
    auth: ReturnType<typeof createAuth>;
  }
}

const authPlugin: FastifyPluginAsync = async (fastify) => {
  const auth = createAuth();

  fastify.decorate("auth", auth);

  fastify.log.info(":white_check_mark: Better-Auth initialized");
};

export default fp(authPlugin, {
  name: "auth",
  dependencies: ["database", "config"],
});

Then this is my route for all-thing-better-auth

import { toNodeHandler } from "better-auth/node";
import type { FastifyInstance } from "fastify";
export async function authRoutes(app: FastifyInstance) {
  app.register(
    (fastify) => {
      fastify.all("*", async (request, reply) => {
        await toNodeHandler(fastify.auth)(request.raw, reply.raw);

        return reply;
      });
    },
    { prefix: "/auth" }
  );
}


which is under /v1
So it's basically /v1/auth/*
My problem is that absolutetly nothing happens when I hit /v1/auth/*
And the error I get is different from the one I'd get if I just hit a route that didn't exited
Was this page helpful?