docs: getting session from the context in db hook (#3051)

* docs: getting session from the context in db hook

* update

* update
This commit is contained in:
KinfeMichael Tariku
2025-07-19 05:03:41 +03:00
committed by GitHub
parent 6515275a1d
commit 3178c5fad8

View File

@@ -570,6 +570,30 @@ export const auth = betterAuth({
});
```
#### Using the Context Object
The context object (`ctx`), passed as the second argument to the hook, contains useful information. For `update` hooks, this includes the current `session`, which you can use to access the logged-in user's details.
```typescript title="auth.ts"
import { betterAuth } from "better-auth";
export const auth = betterAuth({
databaseHooks: {
user: {
update: {
before: async (data, ctx) => {
// You can access the session from the context object.
if (ctx.context.session) {
console.log("User update initiated by:", ctx.context.session.userId);
}
return { data };
},
},
},
},
});
```
Much like standard hooks, database hooks also provide a `ctx` object that offers a variety of useful properties. Learn more in the [Hooks Documentation](/docs/concepts/hooks#ctx).
## Plugins Schema