BA
Better Auth•2mo ago
Ryno

Convex Triggers not firing

I'm using the new convex plugin and seem to be struggling to get triggers to fire in the authComponent.
const siteUrl = process.env.SITE_URL;

export const authComponent = createClient<DataModel>(components.betterAuth, {
triggers: {
user: {
onCreate: async (ctx, user) => {
await ctx.db.insert("profiles", {
userId: user._id,
name: user.name,
email: user.email,
});
},
},
},
});

export const createAuth = (
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false },
) => {
return betterAuth({
logger: {
disabled: optionsOnly,
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
plugins: [convex()],
});
};

export const { onCreate } = authComponent.triggersApi();
const siteUrl = process.env.SITE_URL;

export const authComponent = createClient<DataModel>(components.betterAuth, {
triggers: {
user: {
onCreate: async (ctx, user) => {
await ctx.db.insert("profiles", {
userId: user._id,
name: user.name,
email: user.email,
});
},
},
},
});

export const createAuth = (
ctx: GenericCtx<DataModel>,
{ optionsOnly } = { optionsOnly: false },
) => {
return betterAuth({
logger: {
disabled: optionsOnly,
},
baseURL: siteUrl,
database: authComponent.adapter(ctx),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
plugins: [convex()],
});
};

export const { onCreate } = authComponent.triggersApi();
It seems like despite an account being created, neither of these triggers look like they're firing from what I have been able to tell.
18 Replies
Ryno
RynoOP•2mo ago
Looks like you need to add the authFunctions param to the createClient settings 🙂
Smultar
Smultar•3w ago
I don't follow?
Ryno
RynoOP•3w ago
const authFunctions: AuthFunctions = internal.auth;
export const authComponent = createClient<DataModel>(components.betterAuth, {
authFunctions,
triggers: {
user: {
onCreate: async (ctx, authUser) => {
await ctx.db.insert("posts", {
title: "Hello, world!",
userId: authUser._id,
});
},
onUpdate: async (ctx, oldUser, newUser) => {
// Both old and new documents are available
},
onDelete: async (ctx, authUser) => {
// The entire deleted document is available
},
},
},
});
export const { onCreate, onUpdate, onDelete } = authComponent.triggersApi();
const authFunctions: AuthFunctions = internal.auth;
export const authComponent = createClient<DataModel>(components.betterAuth, {
authFunctions,
triggers: {
user: {
onCreate: async (ctx, authUser) => {
await ctx.db.insert("posts", {
title: "Hello, world!",
userId: authUser._id,
});
},
onUpdate: async (ctx, oldUser, newUser) => {
// Both old and new documents are available
},
onDelete: async (ctx, authUser) => {
// The entire deleted document is available
},
},
},
});
export const { onCreate, onUpdate, onDelete } = authComponent.triggersApi();
The ‘authFunctions’
Smultar
Smultar•3w ago
wheres internal coming from?
Ryno
RynoOP•3w ago
That’s what needed to ensure the triggers work. I think there was some mismatch with types when I was trying so I missed it, but if you first add the exports, then add the auth functions it works Convex generated api Import { internal } from ‘./convex/generated/api’ I believe Sorry on mobile so not positive but should be similar
Smultar
Smultar•3w ago
Im still new to convex, how did you get foreign keys to work, for like auth.user?
Ryno
RynoOP•3w ago
That’s what the triggers are for. Essentially giving context to the auth component records So you add a trigger for the table you want to reference basically
Smultar
Smultar•3w ago
gotcha I've created a new component for my app, where my prefernces live in. I'm trying to reference the better auth userid to the other component. as a foreign key you get any type errors with using the internal.auth?
✖ TypeScript typecheck via `tsc` failed.
To ignore failing typecheck, use `--typecheck=disable`.
convex/auth/index.ts:18:47 - error TS2339: Property 'auth' does not exist on type '{}'.

18 const authFunctions: AuthFunctions = internal.auth;
✖ TypeScript typecheck via `tsc` failed.
To ignore failing typecheck, use `--typecheck=disable`.
convex/auth/index.ts:18:47 - error TS2339: Property 'auth' does not exist on type '{}'.

18 const authFunctions: AuthFunctions = internal.auth;
Ryno
RynoOP•3w ago
@Smultar You need the exports first Then types will work That’s what confused me too
Smultar
Smultar•3w ago
No description
Smultar
Smultar•3w ago
this one OH i understandn ow
Smultar
Smultar•3w ago
Are you using local schemas or the other way for your instances of better convex
Smultar
Smultar•3w ago
@Ryno
Ryno
RynoOP•3w ago
The other way. I haven’t tried hosting anything myself
Smultar
Smultar•3w ago
Local install is mostly using types locally in your app component vs having a separate better with components Not really a physical hosting location @Ryno
Ryno
RynoOP•3w ago
Yeah, I don’t do anything special. I use the generated types, but not the separate generatable schemas and such I basically just followed the convex + better auth docs
Smultar
Smultar•3w ago
Same here, but I'm glad you mentioned this fix for the trigger Have you make an issue to the main repo for convex better auth?
Ryno
RynoOP•3w ago
Nope. I don’t think it is an issue. The exports need to exist for the types to be generated, so it’s working as intended, just a bit odd.

Did you find this page helpful?