Before hook infinite loop

Hello, I am using the before hook in my better-auth config:
import { PrismaClient } from "@/prisma/generated/prisma";
import { stripe } from "@better-auth/stripe";
import { betterAuth } from "better-auth";
import { emailHarmony } from "better-auth-harmony";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { createAuthMiddleware } from "better-auth/api";
import { magicLink } from "better-auth/plugins";
import Stripe from "stripe";

const stripeClient = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: "2025-08-27.basil",
});

const prisma = new PrismaClient();
export const auth = betterAuth({
  database: prismaAdapter(prisma, {
    provider: "postgresql",
  }),
  hooks: {
    before: createAuthMiddleware(async (ctx) => {
      const session = await auth.api.getSession({
        headers: ctx.headers as Headers,
      });
      console.log("Session in middleware:", session);
      if (session?.user) {
        const user = await prisma.user.findUnique({
          where: { id: session.user.id },
          select: { onboardingCompleted: true },
        });
        if (user && !user.onboardingCompleted) {
          return ctx.redirect("/onboarding");
        }
      }
      return ctx;
    }),
  },
  trustedOrigins: [
    "https://mail.google.com",
  ],
  cors: {
    origin: ["http://localhost:3000", "https://mail.google.com"],
    credentials: true,
    allowedHeaders: ["Content-Type", "Authorization"],
    methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
  },
  plugins: [
    stripe({
      stripeClient,
      stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET!,
      createCustomerOnSignUp: true,
    }),
    emailHarmony(),
    magicLink({
      sendMagicLink: async ({ email, token, url }) => {
        console.log(email, token, url);
      },
    }),
  ],
});


but when I load any page, it just runs into an infinite loop but it does compile the page though, anyone has an idea?
Was this page helpful?