mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-24 16:11:53 -05:00
docs: minor imporvements
This commit is contained in:
@@ -118,23 +118,6 @@ export const contents: Content[] = [
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Client",
|
||||
href: "/docs/concepts/client",
|
||||
icon: () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="1.2em"
|
||||
height="1.2em"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M4 8h4V4H4zm6 12h4v-4h-4zm-6 0h4v-4H4zm0-6h4v-4H4zm6 0h4v-4h-4zm6-10v4h4V4zm-6 4h4V4h-4zm6 6h4v-4h-4zm0 6h4v-4h-4z"
|
||||
></path>
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "CLI",
|
||||
icon: () => (
|
||||
@@ -152,6 +135,23 @@ export const contents: Content[] = [
|
||||
),
|
||||
href: "/docs/concepts/cli",
|
||||
},
|
||||
{
|
||||
title: "Client",
|
||||
href: "/docs/concepts/client",
|
||||
icon: () => (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="1.2em"
|
||||
height="1.2em"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
fill="currentColor"
|
||||
d="M4 8h4V4H4zm6 12h4v-4h-4zm-6 0h4v-4H4zm0-6h4v-4H4zm6 0h4v-4h-4zm6-10v4h4V4zm-6 4h4V4h-4zm6 6h4v-4h-4zm0 6h4v-4h-4z"
|
||||
></path>
|
||||
</svg>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Cookies",
|
||||
href: "/docs/concepts/cookies",
|
||||
|
||||
@@ -3,16 +3,17 @@ title: API
|
||||
description: Better Auth API
|
||||
---
|
||||
|
||||
When you create a new Better Auth instance, you get an object called `api` with a set of functions that you can use to interact with the server.
|
||||
When you create a new Better Auth instance, it gives you an `api` object. This object provides functions to interact with the server while your code is running server-side.
|
||||
|
||||
Any endpoint added on the server will be available on the `api` object.
|
||||
Any endpoint added to Better Auth, whether from plugins or the core, will be accessible through the `api` object.
|
||||
|
||||
## Calling API on the Server
|
||||
|
||||
Better Auth uses a library called [better-call](https://github.com/bekacru/better-call) to make API endpoints. It's a library made by the same team behind Better Auth and is designed to work seamlessly with it.Better call endpoints allow you to call `rest` api handlers as if they were normal functions.
|
||||
To call an API endpoint on the server, import your `auth` instance and call the endpoint using the `api` object.
|
||||
|
||||
```ts title="server.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
import { headers } from "next/server";
|
||||
|
||||
export const auth = betterAuth({
|
||||
plugins: [
|
||||
@@ -20,20 +21,6 @@ export const auth = betterAuth({
|
||||
]
|
||||
})
|
||||
|
||||
// calling sign in email on the server
|
||||
await auth.api.signInEmail({
|
||||
body: {
|
||||
email: "",
|
||||
password: ""
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Example: Getting the current Session In Next JS**
|
||||
|
||||
```ts title="server.ts"
|
||||
import { headers } from "next/server";
|
||||
|
||||
// calling get session on the server
|
||||
await auth.api.getSession({
|
||||
headers: headers()
|
||||
@@ -42,6 +29,9 @@ 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.
|
||||
|
||||
<Callout>
|
||||
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.
|
||||
</Callout>
|
||||
|
||||
### Getting the `Response` Object
|
||||
|
||||
|
||||
@@ -29,4 +29,11 @@ pnpm better-auth migrate
|
||||
|
||||
### Options
|
||||
|
||||
- `--config` - The path to your Better Auth config file. By default, the CLI will search for a better-auth.ts file in **./**, **./utils**, **./lib**, or any of these directories under `src` directory.
|
||||
- `--config` - The path to your Better Auth config file. By default, the CLI will search for a better-auth.ts file in **./**, **./utils**, **./lib**, or any of these directories under `src` directory.
|
||||
|
||||
|
||||
## Common Issues
|
||||
|
||||
**Error: Cannot find module "$env/static/.."**
|
||||
|
||||
In sveltekit, if you're facing this error, import the env variable from `import.meta.env` instead.
|
||||
@@ -442,8 +442,9 @@ To add custom fields, use the `additionalFields` property in the `user` or `sess
|
||||
- `type`: The data type of the field (e.g., "string", "number", "boolean").
|
||||
- `required`: A boolean indicating if the field is mandatory.
|
||||
- `defaultValue`: The default value for the field (note: this only applies in the JavaScript layer; in the database, the field will be optional).
|
||||
- `input`: This determines whether a value can be provided when creating a new record (default: `true`). If there are additional fields, like `role`, that should not be provided by the user during signup, you can set this to `false`.
|
||||
|
||||
Here's an example of how to extend the user schema with a custom "role" field:
|
||||
Here's an example of how to extend the user schema with additional fields:
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
@@ -454,31 +455,34 @@ export const auth = betterAuth({
|
||||
role: {
|
||||
type: "string",
|
||||
required: false,
|
||||
defaultValue: "user"
|
||||
defaultValue: "user",
|
||||
input: false // don't allow user to set role
|
||||
},
|
||||
lang: {
|
||||
type: "string",
|
||||
required: false,
|
||||
defaultValue: "en",
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Now you can access the `role` field in your application logic.
|
||||
|
||||
Now you can access the additional fields in your application logic.
|
||||
|
||||
```ts
|
||||
//on signup
|
||||
const res = await auth.api.signUpEmail({
|
||||
email: "test@example.com",
|
||||
password: "password",
|
||||
name: "John Doe",
|
||||
role: "admin"
|
||||
lang: "fr"
|
||||
})
|
||||
|
||||
//user object
|
||||
res.user.role // > "admin"
|
||||
res.user.lang // > "fr"
|
||||
```
|
||||
This configuration will:
|
||||
1. Add a "role" column to the user table in your database (if not already present).
|
||||
2. Ensure that the "role" field is available in user objects returned by Better Auth functions.
|
||||
3. Set a default value of "user" for the "role" field in your application logic.
|
||||
|
||||
<Callout>
|
||||
See the [Typescript](/docs/concepts/typescript#inferring-additional-fields-on-client) documentation for more information on how to infer additional fields on the client side.
|
||||
|
||||
@@ -176,15 +176,15 @@ If your database isn’t supported by Kysely, you can use an adapter to connect.
|
||||
<Step>
|
||||
### Create Database Tables
|
||||
|
||||
Better Auth’s CLI helps manage your tables. You can either migrate tables directly or generate ORM schema/SQL migration files.
|
||||
Better Auth’s comes with a CLI tool to help manage schema required by the library.
|
||||
|
||||
**Migrate**: Creates the required tables in the database.
|
||||
**Migrate**: Directly creates the required tables in the database.
|
||||
|
||||
```bash title="Terminal"
|
||||
npx better-auth migrate
|
||||
```
|
||||
|
||||
**Generate**: Creates an ORM schema or SQL migration file.
|
||||
**Generate**: Generates an ORM schema or SQL migration file.
|
||||
|
||||
```bash title="Terminal"
|
||||
npx better-auth generate
|
||||
@@ -198,6 +198,7 @@ If your database isn’t supported by Kysely, you can use an adapter to connect.
|
||||
</Step>
|
||||
|
||||
<Step>
|
||||
|
||||
### Authentication Methods
|
||||
Configure the authentication methods you want to use. Better Auth comes with built-in support for email/password, and social sign-on providers.
|
||||
|
||||
@@ -220,7 +221,7 @@ export const auth = betterAuth({
|
||||
```
|
||||
|
||||
<Callout type="info">
|
||||
You can use even more authentication methods like passkey, username, magic link and more through plugins.
|
||||
You can use even more authentication methods like [passkey](/docs/plugins/passkey), [username](/docs/plugins/username), [magic link](/docs/plugins/magic-link) and more through plugins.
|
||||
</Callout>
|
||||
</Step>
|
||||
|
||||
@@ -234,7 +235,7 @@ Create a new file or route in your framework's designated catch-all route handle
|
||||
Better Auth supports any backend framework with standard Request and Response objects and offers helper functions for popular frameworks.
|
||||
</Callout>
|
||||
|
||||
<Tabs items={["next-js", "remix", "nuxt", "svelte-kit", "solid-start", "hono", "express"]} defaultValue="react">
|
||||
<Tabs items={["next-js", "nuxt", "svelte-kit", "remix", "solid-start", "hono", "express"]} defaultValue="react">
|
||||
<Tab value="next-js">
|
||||
```ts title="/app/api/auth/[...all]/route.ts"
|
||||
import { auth } from "@/lib/auth"; // path to your auth file
|
||||
|
||||
@@ -3,11 +3,11 @@ title: Introduction
|
||||
description: Introduction to Better Auth.
|
||||
---
|
||||
|
||||
Better Auth is framework-agnostic authentication (and authorization) library for TypeScript. It provides a comprehensive set of features out of the box and includes a plugin ecosystem that simplifies adding advanced functionalities. Whether you need 2FA, multi-tenant support, or other complex features. It lets you focus on building your actual application instead of reinventing the wheel.
|
||||
Better Auth is framework-agnostic authentication (and authorization) framework for TypeScript. It provides a comprehensive set of features out of the box and includes a plugin ecosystem that simplifies adding advanced functionalities. Whether you need 2FA, multi-tenant support, or other complex features. It lets you focus on building your actual application instead of reinventing the wheel.
|
||||
|
||||
## Why Better Auth?
|
||||
|
||||
Authentication in the TypeScript ecosystem is a half-solved problem. Other open-source libraries often requires a lot of additional code for anything beyond basic authentication. Rather than just pushing third-party services as the solution, I believe we can do better as a community—hence, Better Auth.
|
||||
*Authentication in the TypeScript ecosystem is a half-solved problem. Other open-source libraries often requires a lot of additional code for anything beyond basic authentication. Rather than just pushing third-party services as the solution, I believe we can do better as a community—hence, Better Auth.*
|
||||
|
||||
## Features
|
||||
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
"types": "./dist/client/plugins.d.ts"
|
||||
},
|
||||
"./types": "./dist/types.js",
|
||||
"./cli": "./dist/cli.js",
|
||||
"./react": "./dist/react.js",
|
||||
"./preact": "./dist/preact.js",
|
||||
"./solid": "./dist/solid.js",
|
||||
|
||||
@@ -47,7 +47,9 @@ export const migrate = new Command("migrate")
|
||||
process.exit(1);
|
||||
});
|
||||
if (!db) {
|
||||
logger.error("Invalid database configuration.");
|
||||
logger.error(
|
||||
"Invalid database configuration. Make sure you're not using adapters. Migrate command only works with built-in Kysely adapter.",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
const spinner = ora("preparing migration...").start();
|
||||
|
||||
@@ -19,6 +19,11 @@ export type FieldAttributeConfig<T extends FieldType = FieldType> = {
|
||||
* @default true
|
||||
*/
|
||||
returned?: boolean;
|
||||
/**
|
||||
* If a value should be provided when creating a new record.
|
||||
* @default true
|
||||
*/
|
||||
input?: boolean;
|
||||
/**
|
||||
* If the value should be hashed when it's stored.
|
||||
* @default false
|
||||
@@ -131,7 +136,9 @@ export type InferFieldsInput<Field> = Field extends Record<
|
||||
? never
|
||||
: key]: InferFieldInput<Field[key]>;
|
||||
} & {
|
||||
[key in Key]: InferFieldInput<Field[key]> | undefined;
|
||||
[key in Key as Field[key]["input"] extends false ? never : key]:
|
||||
| InferFieldInput<Field[key]>
|
||||
| undefined;
|
||||
}
|
||||
: {};
|
||||
|
||||
|
||||
@@ -119,6 +119,13 @@ export function parseInputData<T extends Record<string, any>>(
|
||||
const parsedData: Record<string, any> = {};
|
||||
for (const key in fields) {
|
||||
if (key in data) {
|
||||
if (fields[key].input === false) {
|
||||
if (fields[key].defaultValue) {
|
||||
parsedData[key] = fields[key].defaultValue;
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
parsedData[key] = data[key];
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user