From 2b3dda4d2e4fbfd76aca6920b2b4cf3a820add06 Mon Sep 17 00:00:00 2001 From: Bereket Engida Date: Wed, 2 Oct 2024 07:29:39 +0300 Subject: [PATCH] docs: db hooks improves --- docs/content/docs/concepts/database.mdx | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/content/docs/concepts/database.mdx b/docs/content/docs/concepts/database.mdx index 7ae6a9bf48..9b925d14ce 100644 --- a/docs/content/docs/concepts/database.mdx +++ b/docs/content/docs/concepts/database.mdx @@ -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 }, }, }, -}; +}) ```