diff --git a/dev/bun/_auth.ts b/dev/bun/_auth.ts deleted file mode 100644 index fa396b16..00000000 --- a/dev/bun/_auth.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { betterAuth } from "better-auth"; -import { prismaAdapter } from "better-auth/adapters/prisma"; -import { APIError } from "better-auth/api"; -import { twoFactor } from "better-auth/plugins"; - -export const auth = betterAuth({ - database: prismaAdapter( - {}, - { - provider: "mysql", - }, - ), - plugins: [twoFactor()], -}); - -try { - await auth.api.signOut(); -} catch (e) { - if (e instanceof APIError) { - } -} diff --git a/dev/bun/auth.ts b/dev/bun/auth.ts index e69de29b..fd5f8cbb 100644 --- a/dev/bun/auth.ts +++ b/dev/bun/auth.ts @@ -0,0 +1,17 @@ +import { betterAuth } from "better-auth"; +import { prismaAdapter } from "better-auth/adapters/prisma"; +import { twoFactor } from "better-auth/plugins"; + +export const auth = betterAuth({ + baseURL: "http://localhost:4000", + database: prismaAdapter( + {}, + { + provider: "sqlite", + }, + ), + emailAndPassword: { + enabled: true, + }, + plugins: [twoFactor()], +}); diff --git a/dev/bun/client.ts b/dev/bun/client.ts new file mode 100644 index 00000000..5533b5c5 --- /dev/null +++ b/dev/bun/client.ts @@ -0,0 +1,15 @@ +import { createAuthClient } from "better-auth/client"; + +await fetch("http://localhost:4000/api/auth/sign-up/email", { + method: "POST", + body: JSON.stringify({ + email: "test-2@test.com", + password: "password", + name: "test-2", + }), + headers: { + "content-type": "application/json", + }, +}) + .then((res) => res.json()) + .then((data) => console.log(data)); diff --git a/dev/bun/index.ts b/dev/bun/index.ts index d397dde6..adaad7e6 100644 --- a/dev/bun/index.ts +++ b/dev/bun/index.ts @@ -1,8 +1,7 @@ -import { auth } from "./_auth"; +import { auth } from "./auth"; Bun.serve({ - fetch(request, server) { - auth; - return new Response("Hello, World!"); - }, + fetch: auth.handler, + port: 4000, }); +console.log("Server running on port 4000"); diff --git a/docs/components/sidebar-content.tsx b/docs/components/sidebar-content.tsx index b669264b..67ab142f 100644 --- a/docs/components/sidebar-content.tsx +++ b/docs/components/sidebar-content.tsx @@ -644,8 +644,8 @@ export const contents: Content[] = [ icon: () => ( -Better Auth uses a library called [better-call](https://github.com/bekacru/better-call) to create API endpoints. Developed by the same team, it's built to integrate seamlessly with Better Auth. With Better Call, you can invoke `rest` API handlers as if they were regular functions. +Better auth API endpoints are built on top of [better-call](https://github.com/bekacru/better-call), a tiny web framework that lets you call REST API endpoints as if they were regular functions and allows us to easily infer client types from the server. ### Getting the `Response` Object @@ -47,7 +55,7 @@ const response = await auth.api.signInEmail({ }) ``` -## Error Handling +### Error Handling When you call an API endpoint in the server, it will throw an error if the request fails. You can catch the error and handle it as you see fit. The error instance is an instance of `APIError`. @@ -66,4 +74,4 @@ try { console.log(error.message, error.status) } } -``` +``` \ No newline at end of file diff --git a/docs/content/docs/concepts/secondary-storage.mdx b/docs/content/docs/concepts/secondary-storage.mdx deleted file mode 100644 index 66bc6dc8..00000000 --- a/docs/content/docs/concepts/secondary-storage.mdx +++ /dev/null @@ -1,5 +0,0 @@ ---- -title: Secondary Storage -description: Using secondary storage with BetterAuth ---- - diff --git a/docs/content/docs/reference/options.mdx b/docs/content/docs/reference/options.mdx index 3b82bd5d..c3aa9aba 100644 --- a/docs/content/docs/reference/options.mdx +++ b/docs/content/docs/reference/options.mdx @@ -250,6 +250,23 @@ list of trusted origins. This will disable CSRF token check for the provided ori /> +### `onAPIError` + +`OnAPIErrorOptions` - Options for handling API errors. + + void' + } + }} +/> ### `logger` diff --git a/package.json b/package.json index 60704044..3f46579e 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "build": "turbo --filter \"./packages/*\" build", "dev": "turbo --filter \"./packages/*\" dev", + "dev:dts": "turbo --filter \"./packages/*\" dev:dts", "clean": "turbo --filter \"./packages/*\" clean && rm -rf node_modules", "format": "biome format . --write", "lint": "biome check .", diff --git a/packages/better-auth/src/api/index.ts b/packages/better-auth/src/api/index.ts index f55eec95..1de1eeec 100644 --- a/packages/better-auth/src/api/index.ts +++ b/packages/better-auth/src/api/index.ts @@ -219,6 +219,14 @@ export const router = ( return res; }, onError(e) { + if (options.onAPIError?.throw) { + throw e; + } + if (options.onAPIError?.onError) { + options.onAPIError.onError(e, ctx); + return; + } + const log = options.logger?.verboseLogging ? logger : undefined; if (options.logger?.disabled !== true) { if (e instanceof APIError) { @@ -230,7 +238,7 @@ export const router = ( if (typeof e === "object" && e !== null && "message" in e) { const errorMessage = e.message as string; if (!errorMessage || typeof errorMessage !== "string") { - log?.error(e); + logger?.error(e); return; } if (errorMessage.includes("no such table")) { @@ -258,10 +266,10 @@ export const router = ( )} to create the tables. There are missing tables in your MySQL database.`, ); } else { - log?.error(e); + logger?.error(e); } } else { - log?.error(e); + logger?.error(e); } } } diff --git a/packages/better-auth/src/types/options.ts b/packages/better-auth/src/types/options.ts index 947235e2..24e2e8f6 100644 --- a/packages/better-auth/src/types/options.ts +++ b/packages/better-auth/src/types/options.ts @@ -8,7 +8,7 @@ import type { BetterSqlite3Database, MysqlPool } from "./database"; import type { KyselyDatabaseType } from "../adapters/kysely-adapter/types"; import type { FieldAttribute } from "../db"; import type { RateLimit } from "./models"; -import type { EligibleCookies } from "../cookies"; +import type { AuthContext } from "."; export interface BetterAuthOptions { /** @@ -436,4 +436,22 @@ export interface BetterAuthOptions { }; }; }; + /** + * API error handling + */ + onAPIError?: { + /** + * Throw an error on API error + * + * @default false + */ + throw?: boolean; + /** + * Custom error handler + * + * @param error + * @param ctx - Auth context + */ + onError?: (error: unknown, ctx: AuthContext) => void | Promise; + }; }