docs: db hooks improves

This commit is contained in:
Bereket Engida
2024-10-02 07:29:47 +03:00
parent ae64245b7a
commit 2b3dda4d2e
+8 -12
View File
@@ -282,9 +282,7 @@ There are two types of hooks you can define:
### 1. Before Hook
- **Purpose**: This hook is called before the respective entity (user, session, or account) is created or updated.
- **Behavior**:
- If the hook returns `false`, the operation will be aborted.
- If it returns an object, that object will replace the original data.
- **Behavior**: If the hook returns `false`, the operation will be aborted. And If it returns an object, that object will replace the original data.
### 2. After Hook
@@ -293,9 +291,11 @@ There are two types of hooks you can define:
**Example Usage: add a `before` hook to the `user` model to split the `name` field into `firstName` and `lastName` fields.**
```typescript
const hooks: BetterAuthOptions['databaseHooks'] = {
user: {
```typescript title="auth.ts"
import { betterAuth } from "better-auth";
export const auth = betterAuth({
user: {
create: {
before: async (user) => {
return {
@@ -307,13 +307,9 @@ const hooks: BetterAuthOptions['databaseHooks'] = {
},
update: {
after: async (user) => {
return {
...user,
firstName: user.name.split(" ")[0],
lastName: user.name.split(" ")[1]
}
//perform additional actions, like creating a stripe customer
},
},
},
};
})
```