cli not generating migration files due to cf bindings usage

im trying to set up better auth on cf workers (currently with hono, but would switch once this i would find a fix for this). i have the following betterauth config:
// auth.ts
import { betterAuth } from "better-auth";
import { z } from "zod";

export const createAuth = (env: Env) =>
betterAuth({
basePath: "/api/auth",
database: env.AUTH_DB,
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
},
user: {
additionalFields: {
role: {
type: "string",
validator: {
input: z.enum(["guest", "user", "admin"]).default("guest"),
},
},
active: {
type: "boolean",
validator: {
input: z.boolean(),
},
defaultValue: false,
},
},
},
});
// auth.ts
import { betterAuth } from "better-auth";
import { z } from "zod";

export const createAuth = (env: Env) =>
betterAuth({
basePath: "/api/auth",
database: env.AUTH_DB,
socialProviders: {
google: {
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
},
user: {
additionalFields: {
role: {
type: "string",
validator: {
input: z.enum(["guest", "user", "admin"]).default("guest"),
},
},
active: {
type: "boolean",
validator: {
input: z.boolean(),
},
defaultValue: false,
},
},
},
});
it has to be a function as i need to pass the cf env in somehow. this is where the client is created:
// index.ts
import { Hono } from "hono";
import { cors } from "hono/cors";
import { createAuth } from "./auth";

const app = new Hono<{ Bindings: Env }>();

app.use("/api/auth/*", async (c, next) => {
const corsMiddlewareHandler = cors({
origin: c.env.ALLOWED_ORIGIN,
allowHeaders: ["Content-Type", "Authorization"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length"],
maxAge: 600,
credentials: true,
});
return corsMiddlewareHandler(c, next);
});

app.on(["POST", "GET"], "/api/auth/*", (c) => {
return createAuth(c.env).handler(c.req.raw);
});

export default app satisfies ExportedHandler<Env>;
// index.ts
import { Hono } from "hono";
import { cors } from "hono/cors";
import { createAuth } from "./auth";

const app = new Hono<{ Bindings: Env }>();

app.use("/api/auth/*", async (c, next) => {
const corsMiddlewareHandler = cors({
origin: c.env.ALLOWED_ORIGIN,
allowHeaders: ["Content-Type", "Authorization"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length"],
maxAge: 600,
credentials: true,
});
return corsMiddlewareHandler(c, next);
});

app.on(["POST", "GET"], "/api/auth/*", (c) => {
return createAuth(c.env).handler(c.req.raw);
});

export default app satisfies ExportedHandler<Env>;
has anyone experienced this aswell before? im aware that the cli is trying to look for a config exported under the name auth, but under my circumstances it's simply just not possible
Solution:
I recommend making a fake auth file and a real auth file. The fake one is just your better-auth config except for any fields which require ENV vars. ^ This file will be the one you would then use for the better-auth cli to read. The real one would be the one you currently have....
Jump to solution
11 Replies
Solution
Ping
Ping3w ago
I recommend making a fake auth file and a real auth file. The fake one is just your better-auth config except for any fields which require ENV vars. ^ This file will be the one you would then use for the better-auth cli to read. The real one would be the one you currently have.
Ping
Ping3w ago
@mezo
mezo
mezoOP3w ago
sounds good. is there a plan to have an actual fix for this? is something like ReturnType<typeof createAuth> not valid?
Ping
Ping3w ago
We run the code. We don't infer types and determine from there. It's a cloudflare limitation, I don't think we will be making a solution for this.
mezo
mezoOP3w ago
i see how would i create a mock d1 database? would it just be sqlite?
hffmnn_
hffmnn_2w ago
Looking at the docs, env can also be imported like this: import { env } from "cloudflare:workers"; so eventually you don't need to make this dynamic.
Cloudflare Docs
Bindings (env) · Cloudflare Workers docs
Worker Bindings that allow for interaction with other Cloudflare Resources.
Ping
Ping2w ago
database shouldn't be an env variable
mezo
mezoOP2w ago
welcome to cloudflare doesnt work, module undefined as we are not in cf context when generating the schema
hffmnn_
hffmnn_2w ago
I guess what @Ping meant with database shouldn't be an env variable: you can't put an env variable into the database property of better-auth. You have to e.g. use drizzle (especially since drizzle supports D1): https://www.better-auth.com/docs/adapters/drizzle https://orm.drizzle.team/docs/connect-cloudflare-d1
Drizzle ORM Adapter | Better Auth
Integrate Better Auth with Drizzle ORM.
Drizzle ORM - Cloudflare D1
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Ping
Ping2w ago
Bingo
mezo
mezoOP2w ago
ah yeah realized that sometime after

Did you find this page helpful?