update(docs): example code to throw errors in database hooks (#790)

This commit is contained in:
Multinite
2024-12-07 06:37:47 +10:00
committed by GitHub
parent c2e03d5a16
commit 24e12de95a

View File

@@ -711,6 +711,35 @@ export const auth = betterAuth({
})
```
#### Throwing Errors
If you want to stop the database hook from proceeding, you can throw errors using the `APIError` class imported from `better-auth/api`.
```typescript title="auth.ts"
import { betterAuth } from "better-auth";
import { APIError } from "better-auth/api";
export const auth = betterAuth({
databaseHooks: {
user: {
create: {
before: async (user) => {
if(user.isAgreedToTerms === false) { // Your special condition.
// Send the API error.
throw new APIError("BAD_REQUEST", {
message: "User must agree to the TOS before signing up.",
});
}
return {
data: user
};
},
},
},
}
})
```
## Plugins Schema
Plugins can define their own tables in the database to store additional data. They can also add columns to the core tables to store additional data. For example, the two factor authentication plugin adds the following columns to the `user` table: