mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-30 02:46:44 -05:00
update(docs): example code to throw errors in database hooks (#790)
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user