accessing plugins in DB hooks
Just a question if it's possible or not to access plugins in the DB hooks. good example is creating an organisation when a user signs up?
looked around the docs and couldn't find much
15 Replies
check api path from console when user create and create a database hook with that path
Yes you can.
Do you have an example?
I have something like this,
why not just use auth.api.createOrganization?
because auth isn't instantiated yet..
It's like
const config = {
// better auth config here
}
export const auth = betterAuth(config);
You're going to need to move the databaseHook part of your config straight into
betterAuth
, this way you can access auth
Such as?
Solution

I may be missing something, but the plugin isn't registered yet?

Mimicked your setup, everything works for me.
Could it be the type issues regarding the
after
function causing it?
Yeah I can't access it inside the hook but can access it when its called after
const auth = betterAuth();
auth.api.createOrganization // works
Not sure why you're having this issue, but this flow is very common. I often see people create orgs right in the DB hooks. 🤔
I wonder if it's my tsconfig, I'm using t3-turbo as the base of my project if that helps?
How do we pass the headers? is there a way to infer the headers from inside the after function to pass them on?
No, however some apis such as createOrganization can be ran without headers as long as you provide a userID
Yeah that makes sense and is working now