Using better-auth with HttpApi and encountering a bug

I have a weird bug, I'm using better-auth with the HttpApi, I have an auth-router:
import {
  HttpApiBuilder,
  HttpServerRequest,
  HttpServerResponse,
} from "@effect/platform";
import { BunHttpServerRequest } from "@effect/platform-bun";
import { Auth } from "@tryelement/auth/server";
import { Api } from "@tryelement/domain";
import { BetterAuthApiError } from "@tryelement/domain/auth";
import { Effect } from "effect";
import { EnvVars } from "@/services/env-vars-service";

const betterAuthHandler = Effect.gen(function* () {
  const request = yield* HttpServerRequest.HttpServerRequest;
  const req = BunHttpServerRequest.toRequest(request);

  const { auth } = yield* Auth;
  const { APP_URL } = yield* EnvVars;
  const res = yield* Effect.tryPromise({
    try: () => auth.handler(req),
    catch: (cause) => {
      return new BetterAuthApiError({ cause });
    },
  }).pipe(Effect.tapError(Effect.logError));

  res.headers.set("Access-Control-Allow-Origin", APP_URL);
  res.headers.set("Access-Control-Allow-Methods", "GET, POST");
  res.headers.set("Access-Control-Allow-Headers", "Content-Type");
  res.headers.set("Access-Control-Allow-Credentials", "true");

  return HttpServerResponse.raw(res);
});

export const AuthRouter = HttpApiBuilder.group(
  Api,
  "auth",
  Effect.fnUntraced(function* (handlers) {
    return handlers
      .handleRaw("get", () => betterAuthHandler)
      .handleRaw("post", () => betterAuthHandler);
  })
);


That works great!
Was this page helpful?