mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-25 10:13:54 -05:00
docs: db hooks improves
This commit is contained in:
@@ -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
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
})
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user