This commit is contained in:
Bereket Engida
2024-10-05 23:21:05 +03:00
parent ef7f3318bf
commit 01affea438
3 changed files with 30 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
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({
@@ -11,3 +12,10 @@ export const auth = betterAuth({
),
plugins: [twoFactor()],
});
try {
await auth.api.signOut();
} catch (e) {
if (e instanceof APIError) {
}
}

View File

@@ -42,3 +42,24 @@ await auth.api.getSession({
Unlike the client, the server needs the values to be passed as an object with the key `body` for the body, `headers` for the headers, and `query` for the query.
## 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`.
```ts title="server.ts"
import { APIError } from "better-auth/api";
try {
await auth.api.signInEmail({
body: {
email: "",
password: ""
}
})
} catch (error) {
if (error instanceof APIError) {
console.log(error.message, error.status)
}
}
```

View File

@@ -254,3 +254,4 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
export * from "./routes";
export * from "./middlewares";
export * from "./call";
export { APIError } from "better-call";