encore icon indicating copy to clipboard operation
encore copied to clipboard

Using Better-Auth CLI with Encore

Open Maraket opened this issue 10 months ago • 4 comments

Issue

When attempting to run pnpx @better-auth/cli generate I get the error:

2025-01-11T09:26:32.971Z ERROR [Better Auth]: Couldn't read your auth config. Error: The ENCORE_RUNTIME_LIB environment variable is not set. It must be set to the path of the Encore runtime library ('encore-runtime.node').

Current Investigation

After finding https://github.com/encoredev/encore/issues/1611, I understand I can't simply add the env variable to solve the issue.

I found I can do the schema manually, as per https://better-auth.vercel.app/docs/concepts/database#core-schema, though this both bypasses the issue, and wouldn't resolve the issue if I opted to use the migrate command for better-auth

Expectation

Potentially a way to run commands in context to the running encore application.

Currently my stack

System

  • Node Version: 20.18.0
  • Encore Version: 1.45.6
  • OS: MacOS 14.2.1

Maraket avatar Jan 11 '25 11:01 Maraket

Got the same problem, impossible to generate any schema from the better-auth cli nor the drizzle cli... I did add the ENCORE_RUNTIME_LIB to my env, but now I get the following error... failed to initialize runtime: unable to parse app metadata file

carere avatar Feb 17 '25 00:02 carere

Did you manage to find a fix? @carere

etxdev avatar Mar 14 '25 00:03 etxdev

The problem is you have coded it something like

import { db } from "./encore.service";

// Initialize Drizzle ORM with the connection string
const connString = db.connectionString
export const orm = drizzle(connString, {
  schema: {
    ...authSchema
  }
});

And this file will later be used by betterAuth and drizzle to determine what has changed in the database (ie, it make a request to the database, db.connectionString). Unfortunately db is defined in encore.service like this

export const db = new SQLDatabase("user", {
  migrations: {
    path: "migrations",
    source: "drizzle",
  },
});

The db variable is provided by encore runtime.

Creating the migration using pnpx @better-auth/cli generate, bypassed the encore runtime (it does not call encore run), and therefore the db doesn't exists

To workaround this issue I run encore run in one terminal, then in other terminal i run encore db conn-uri user, which gave me a connection string like postgresql://abcdef:[email protected]:9500/user?sslmode=disable

I put it directly into my drizzle initialization like so

// import { db } from "./encore.service";

// Initialize Drizzle ORM with the connection string
const connString = "postgresql://abcdef:[email protected]:9500/user?sslmode=disable"
export const orm = drizzle(connString, {
  schema: {
    ...authSchema
  }
});

Then, I call pnpx @better-auth/cli generate

hariangr avatar Mar 18 '25 01:03 hariangr

We've added a encore exec command that will setup the encore infrastructure and environment variables for your app, and then execute a command.

E.g like this

encore exec -- pnpx @better-auth/cli generate

Its available in encore version 1.46.16

fredr avatar Apr 02 '25 07:04 fredr