2025-10-05 20:21:45 +00:00
|
|
|
import { createClient, type GenericCtx } from "@convex-dev/better-auth"
|
|
|
|
|
import { convex, crossDomain } from "@convex-dev/better-auth/plugins"
|
|
|
|
|
import { betterAuth } from "better-auth"
|
2025-10-18 19:32:05 +00:00
|
|
|
import { components } from "@fileone/convex/api"
|
|
|
|
|
import type { DataModel } from "@fileone/convex/dataModel"
|
2025-10-05 23:25:20 +00:00
|
|
|
import authSchema from "./betterauth/schema"
|
2025-10-05 20:21:45 +00:00
|
|
|
|
|
|
|
|
const siteUrl = process.env.SITE_URL!
|
|
|
|
|
|
|
|
|
|
// The component client has methods needed for integrating Convex with Better Auth,
|
|
|
|
|
// as well as helper methods for general use.
|
2025-10-05 23:25:20 +00:00
|
|
|
export const authComponent = createClient<DataModel, typeof authSchema>(
|
|
|
|
|
components.betterAuth,
|
|
|
|
|
{
|
|
|
|
|
local: {
|
|
|
|
|
schema: authSchema,
|
|
|
|
|
},
|
|
|
|
|
triggers: {
|
|
|
|
|
user: {
|
|
|
|
|
onCreate: async (ctx, user) => {
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
await ctx.db.insert("directories", {
|
|
|
|
|
name: "",
|
|
|
|
|
userId: user._id,
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
})
|
|
|
|
|
},
|
2025-10-05 22:14:44 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-10-05 23:25:20 +00:00
|
|
|
)
|
2025-10-05 20:21:45 +00:00
|
|
|
|
|
|
|
|
export const createAuth = (
|
|
|
|
|
ctx: GenericCtx<DataModel>,
|
|
|
|
|
{ optionsOnly } = { optionsOnly: false },
|
|
|
|
|
) => {
|
|
|
|
|
return betterAuth({
|
|
|
|
|
// disable logging when createAuth is called just to generate options.
|
|
|
|
|
// this is not required, but there's a lot of noise in logs without it.
|
|
|
|
|
logger: {
|
|
|
|
|
disabled: optionsOnly,
|
|
|
|
|
},
|
|
|
|
|
trustedOrigins: [siteUrl],
|
|
|
|
|
database: authComponent.adapter(ctx),
|
|
|
|
|
// Configure simple, non-verified email/password to get started
|
|
|
|
|
emailAndPassword: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
requireEmailVerification: false,
|
|
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
// The cross domain plugin is required for client side frameworks
|
|
|
|
|
crossDomain({ siteUrl }),
|
|
|
|
|
// The Convex plugin is required for Convex compatibility
|
|
|
|
|
convex(),
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
}
|