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
* New reference architecture diagram: Storing user generated content
* Apply suggestions from code review
* Fix broken links
* Update fullstack authentication tutorial for Next.js and Cloudflare D1
- Clarified instructions for modifying `tsconfig.json` and `cloudflare-env.d.ts`.
- Added note about wrangler creating the worker if not deployed.
- Improved error handling in the GET function.
- Updated file references for clarity and consistency.
* Update fullstack authentication tutorial for Next.js. Adjusted the tutorial's update date, refined package manager arguments, and added notes on D1 database creation and Windows support.
* Refactor getCloudflareContext calls to use async option
---------
Co-authored-by: Jun Lee <[email protected]>
Copy file name to clipboardExpand all lines: src/content/docs/developer-spotlight/tutorials/fullstack-authentication-with-next-js-and-cloudflare-d1.mdx
+30-20Lines changed: 30 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
updated: 2025-01-13
2
+
updated: 2025-04-21
3
3
difficulty: Intermediate
4
4
pcx_content_type: tutorial
5
5
title: Setup Fullstack Authentication with Next.js, Auth.js, and Cloudflare D1
@@ -46,7 +46,7 @@ From within the repository or directory where you want to create your project ru
@@ -60,7 +60,7 @@ From within the repository or directory where you want to create your project ru
60
60
61
61
This will create a new Next.js project using [OpenNext](https://opennext.js.org/cloudflare) that will run in a Worker using [Workers Static Assets](/workers/frameworks/framework-guides/nextjs/#static-assets).
62
62
63
-
Before we get started, open your project's `tsconfig.json` file and add the following to the `compilerOptions` object to allow for top level await needed to let our application get the Cloudflare context:
63
+
Before we get started, open your project's `tsconfig.json` file and modify the following to the `compilerOptions` object to allow for top level await needed to let our application get the Cloudflare context:
64
64
65
65
```json title="tsconfig.json"
66
66
{
@@ -101,6 +101,8 @@ Now, deviating from the standard Auth.js setup, locate your generated secret (li
101
101
npx wrangler secret put AUTH_SECRET
102
102
```
103
103
104
+
If you have not deployed yet that's fine. Allow wrangler to create the worker for you.
105
+
104
106
After adding the secret, update your `.dev.vars` file to include an `AUTH_SECRET` value (this secret should be different from the one you generated earlier for security purposes):
105
107
106
108
```sh title=".dev.vars"
@@ -109,9 +111,9 @@ AUTH_SECRET = "<replace-me>"
109
111
# ...
110
112
```
111
113
112
-
Next, go into the newly generated `env.d.ts` file and add the following to the <Typetext="CloudflareEnv" /> interface:
114
+
Next, go into `cloudflare-env.d.ts` file and add the following to the <Typetext="CloudflareEnv" /> interface:
Now, within the `src/` directory, create a `middleware.ts` file to persist session data containing the following:
239
+
Now, within the `src/` directory, create a `middleware.ts` file. If you do not have a `src/` directory, create a `middleware.ts` file in the root of your project. This will persist session data.
238
240
239
241
<TypeScriptExamplefilename="src/middleware.ts">
240
242
```ts
@@ -248,15 +250,17 @@ The D1 adapter requires that tables be created within your database. It [recomme
@@ -420,6 +424,8 @@ Now, it's time to preview our app. Run the following to preview your application
420
424
421
425
:::caution[Windows support]
422
426
OpenNext has [limited Windows support](https://opennext.js.org/cloudflare#windows-support) and recommends using WSL2 if developing on Windows.
427
+
428
+
Also, you may need to comment out the `@import "tw-animate-css"` line in the `globals.css` file.
423
429
:::
424
430
425
431
You should see our login form. But wait, we're not done yet. Remember to create your database tables by visiting `/api/setup`. You should see `Migration completed`. This means your database is ready to go.
@@ -441,6 +447,10 @@ npx wrangler secret put AUTH_URL
441
447
442
448
After the changes are deployed, you should now be able to access and try out your new application.
443
449
450
+
:::note[D1 Database Creation]
451
+
You will need to hit the `/api/setup` on your deployed URL to create the necessary tables in your D1 database. It will create 4 tables if they don’t already exist: `accounts`, `sessions`, `users`, and `verification_tokens`. If the `api/setup` route is not working, you can also initialize your tables manually. Look in [migrations.ts](https://github.com/nextauthjs/next-auth/blob/main/packages/adapter-d1/src/migrations.ts) of the Auth.js D1 adapter for the relevant SQL.
452
+
:::
453
+
444
454
You have successfully created, configured, and deployed a fullstack Next.js application with authentication powered by Auth.js, Resend, and Cloudflare D1.
0 commit comments