This is a database adapter for Convex that allows you to use BetterAuth with Convex.
npm install convex-database-adapter
Head over to your Better Auth server instance, and under database
, add the convexAdapter
function.
import { betterAuth } from "better-auth";
import { convexAdapter } from "convex-better-auth";
export const auth = betterAuth({
database: convexAdapter({
convex_url: process.env.CONVEX_URL,
}),
plugins: [],
//... other options
});
This allows our adapter to communicate with your Convex DB.
Create a new file in convex/betterAuth.ts
and add the following code:
NOTE: It's important that the file name is exactly betterAuth.ts
and that it is in the convex
directory.
import { action, internalQuery } from "./_generated/server";
import { internal } from "./_generated/api";
import { ConvexHandler, ConvexReturnType } from "./../src/convex_action";
const { betterAuth, query } = ConvexHandler({
action,
internalQuery,
internal,
}) as ConvexReturnType;
export { betterAuth, query };