You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Better-Auth](https://better-auth.com/) is a modern, open-source authentication solution for web applications. It's built with TypeScript and provides a simple and extensible auth experience with support for multiple database adapters, including Prisma.
13
+
[BetterAuth](https://better-auth.com/) is a modern, open-source authentication solution for web applications. It's built with TypeScript and provides a simple and extensible auth experience with support for multiple database adapters, including Prisma.
14
14
15
-
In this guide, you'll wire Better-Auth into a brand-new [Next.js](https://nextjs.org/) app and persist users in a [Prisma Postgres](https://prisma.io/postgres) database. You can find a complete example of this guide on [GitHub](https://github.com/prisma/prisma-examples/tree/latest/orm/betterauth-nextjs).
15
+
In this guide, you'll wire BetterAuth into a brand-new [Next.js](https://nextjs.org/) app and persist users in a [Prisma Postgres](https://prisma.io/postgres) database. You can find a complete example of this guide on [GitHub](https://github.com/prisma/prisma-examples/tree/latest/orm/betterauth-nextjs).
16
16
17
17
## Prerequisites
18
18
@@ -78,7 +78,7 @@ Once installed, initialize Prisma in your project:
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My Better-Auth Project"
81
+
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My BetterAuth Project"
82
82
:::
83
83
84
84
This will create:
@@ -149,19 +149,19 @@ We recommend using a connection pooler (like [Prisma Accelerate](https://www.pri
149
149
If you choose not to use one, **avoid** instantiating `PrismaClient` globally in long-lived environments. Instead, create and dispose of the client per request to prevent exhausting your database connections.
150
150
:::
151
151
152
-
## 3. Set up Better-Auth
152
+
## 3. Set up BetterAuth
153
153
154
-
Now it's time to integrate Better-Auth for authentication.
154
+
Now it's time to integrate BetterAuth for authentication.
155
155
156
-
### 3.1. Install and configure Better-Auth
156
+
### 3.1. Install and configure BetterAuth
157
157
158
-
First, install the Better-Auth core package:
158
+
First, install the BetterAuth core package:
159
159
160
160
```terminal
161
161
npm install better-auth
162
162
```
163
163
164
-
Next, generate a secure secret that Better-Auth will use to sign authentication tokens. This ensures your tokens cannot be messed with.
164
+
Next, generate a secure secret that BetterAuth will use to sign authentication tokens. This ensures your tokens cannot be messed with.
Now, create a configuration file for Better-Auth. In the `src/lib` directory, create an `auth.ts` file:
183
+
Now, create a configuration file for BetterAuth. In the `src/lib` directory, create an `auth.ts` file:
184
184
185
185
```terminal
186
186
touch src/lib/auth.ts
187
187
```
188
188
189
-
In this file, you'll configure Better-Auth to use the Prisma adapter, which allows it to persist user and session data in your database. You will also enable email and password authentication.
189
+
In this file, you'll configure BetterAuth to use the Prisma adapter, which allows it to persist user and session data in your database. You will also enable email and password authentication.
Better-Auth also supports other sign-in methods like social logins (Google, GitHub, etc.), which you can explore in their [documentation](https://www.better-auth.com/docs/authentication/email-password).
203
+
BetterAuth also supports other sign-in methods like social logins (Google, GitHub, etc.), which you can explore in their [documentation](https://www.better-auth.com/docs/authentication/email-password).
Better-Auth provides a CLI command to automatically add the necessary authentication models (`User`, `Session`, `Account`, and `Verification`) to your `schema.prisma` file.
245
+
BetterAuth provides a CLI command to automatically add the necessary authentication models (`User`, `Session`, `Account`, and `Verification`) to your `schema.prisma` file.
246
246
247
247
Run the following command:
248
248
@@ -329,7 +329,7 @@ npx prisma migrate dev --name add-auth-models
329
329
330
330
## 4. Set up the API routes
331
331
332
-
Better-Auth needs an API endpoint to handle authentication requests like sign-in, sign-up, and sign-out. You'll create a catch-all API route in Next.js to handle all requests sent to `/api/auth/[...all]`.
332
+
BetterAuth needs an API endpoint to handle authentication requests like sign-in, sign-up, and sign-out. You'll create a catch-all API route in Next.js to handle all requests sent to `/api/auth/[...all]`.
333
333
334
334
In the `src/app/api` directory, create an `auth/[...all]` folder structure and a `route.ts` file inside it:
Add the following code to the newly created `route.ts` file. This code uses a helper from Better-Auth to create Next.js-compatible `GET` and `POST` request handlers.
341
+
Add the following code to the newly created `route.ts` file. This code uses a helper from BetterAuth to create Next.js-compatible `GET` and `POST` request handlers.
342
342
343
343
```ts
344
344
import { auth } from"@/lib/auth";
@@ -414,7 +414,7 @@ export default function SignUpPage() {
414
414
}
415
415
```
416
416
417
-
Now, import the `signUp` function from your Better-Auth client and add the `handleSubmit` function. This function is triggered on form submission and calls the `signUp.email` method provided by Better-Auth, passing the user's name, email, and password.
417
+
Now, import the `signUp` function from your BetterAuth client and add the `handleSubmit` function. This function is triggered on form submission and calls the `signUp.email` method provided by BetterAuth, passing the user's name, email, and password.
418
418
419
419
```tsx file=src/app/sign-up/page.tsx
420
420
"use client";
@@ -615,7 +615,7 @@ export default function SignInPage() {
615
615
}
616
616
```
617
617
618
-
Add the `handleSubmit` function, this time importing and using the `signIn.email` method from Better-Auth.
618
+
Add the `handleSubmit` function, this time importing and using the `signIn.email` method from BetterAuth.
619
619
620
620
```tsx file=src/app/sign-in/page.tsx
621
621
"use client";
@@ -782,7 +782,7 @@ export default function DashboardPage() {
[Better-Auth](https://better-auth.com/) is a modern, open-source authentication solution for web applications. It's built with TypeScript and provides a simple and extensible auth experience with support for multiple database adapters, including Prisma.
13
+
[BetterAuth](https://better-auth.com/) is a modern, open-source authentication solution for web applications. It's built with TypeScript and provides a simple and extensible auth experience with support for multiple database adapters, including Prisma.
14
14
15
-
In this guide, you'll wire Better-Auth into a brand-new [Astro](https://astro.build/) app and persist users in a [Prisma Postgres](https://prisma.io/postgres) database. You can find a complete example of this guide on [GitHub](https://github.com/prisma/prisma-examples/tree/latest/orm/betterauth-astro).
15
+
In this guide, you'll wire BetterAuth into a brand-new [Astro](https://astro.build/) app and persist users in a [Prisma Postgres](https://prisma.io/postgres) database. You can find a complete example of this guide on [GitHub](https://github.com/prisma/prisma-examples/tree/latest/orm/betterauth-astro).
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My Better-Auth Astro Project"
75
+
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database like "My BetterAuth Astro Project"
76
76
:::
77
77
78
78
This will create:
@@ -156,19 +156,19 @@ We recommend using a connection pooler (like [Prisma Accelerate](https://www.pri
156
156
If you choose not to use one, **avoid** instantiating `PrismaClient` globally in long-lived environments. Instead, create and dispose of the client per request to prevent exhausting your database connections.
157
157
:::
158
158
159
-
## 3. Set up Better-Auth
159
+
## 3. Set up BetterAuth
160
160
161
-
Now it's time to integrate Better-Auth for authentication.
161
+
Now it's time to integrate BetterAuth for authentication.
162
162
163
-
### 3.1. Install and configure Better-Auth
163
+
### 3.1. Install and configure BetterAuth
164
164
165
-
First, install the Better-Auth core package:
165
+
First, install the BetterAuth core package:
166
166
167
167
```terminal
168
168
npm install better-auth
169
169
```
170
170
171
-
Next, generate a secure secret that Better-Auth will use to sign authentication tokens. This ensures your tokens cannot be tampered with.
171
+
Next, generate a secure secret that BetterAuth will use to sign authentication tokens. This ensures your tokens cannot be tampered with.
Astro's default development server runs on port `4321`. If your application runs on a different port, update the `BETTER_AUTH_URL` accordingly.
192
192
:::
193
193
194
-
Now, create a configuration file for Better-Auth. In the `src/lib` directory, create an `auth.ts` file:
194
+
Now, create a configuration file for BetterAuth. In the `src/lib` directory, create an `auth.ts` file:
195
195
196
196
```terminal
197
197
touch src/lib/auth.ts
198
198
```
199
199
200
-
In this file, you'll configure Better-Auth to use the Prisma adapter, which allows it to persist user and session data in your database. You will also enable email and password authentication.
200
+
In this file, you'll configure BetterAuth to use the Prisma adapter, which allows it to persist user and session data in your database. You will also enable email and password authentication.
Better-Auth also supports other sign-in methods like social logins (Google, GitHub, etc.), which you can explore in their [documentation](https://www.better-auth.com/docs/authentication/email-password).
217
+
BetterAuth also supports other sign-in methods like social logins (Google, GitHub, etc.), which you can explore in their [documentation](https://www.better-auth.com/docs/authentication/email-password).
218
218
219
-
### 3.2. Add Better-Auth models to your schema
219
+
### 3.2. Add BetterAuth models to your schema
220
220
221
-
Better-Auth provides a CLI command to automatically add the necessary authentication models (`User`, `Session`, `Account`, and `Verification`) to your `schema.prisma` file.
221
+
BetterAuth provides a CLI command to automatically add the necessary authentication models (`User`, `Session`, `Account`, and `Verification`) to your `schema.prisma` file.
222
222
223
223
Run the following command:
224
224
@@ -306,7 +306,7 @@ npx prisma migrate dev --name add-auth-models
306
306
307
307
## 4. Set up the API routes
308
308
309
-
Better-Auth needs an API endpoint to handle authentication requests like sign-in, sign-up, and sign-out. You'll create a catch-all API route in Astro to handle all requests sent to `/api/auth/[...all]`.
309
+
BetterAuth needs an API endpoint to handle authentication requests like sign-in, sign-up, and sign-out. You'll create a catch-all API route in Astro to handle all requests sent to `/api/auth/[...all]`.
310
310
311
311
In the `src/pages` directory, create an `api/auth` folder structure and a `[...all].ts` file inside it:
312
312
@@ -315,7 +315,7 @@ mkdir -p src/pages/api/auth
315
315
touch 'src/pages/api/auth/[...all].ts'
316
316
```
317
317
318
-
Add the following code to the newly created `[...all].ts` file. This code uses the Better-Auth handler to process authentication requests.
318
+
Add the following code to the newly created `[...all].ts` file. This code uses the BetterAuth handler to process authentication requests.
Now add a script to handle form submission. Import the `authClient` and add an event listener to the form that prevents the default submission behavior, extracts the form data, and calls the Better-Auth sign-up method.
475
+
Now add a script to handle form submission. Import the `authClient` and add an event listener to the form that prevents the default submission behavior, extracts the form data, and calls the BetterAuth sign-up method.
Now add a script to handle form submission. Import the `authClient` and add an event listener that prevents default submission, extracts the form data, and calls the Better-Auth sign-in method.
642
+
Now add a script to handle form submission. Import the `authClient` and add an event listener that prevents default submission, extracts the form data, and calls the BetterAuth sign-in method.
0 commit comments